#!/bin/sh
# change this to #!/bin/ksh for AIX

###########################################################################
# Main Script Logic - Start/Stop Script for Domino on xLinux/zLinux/AIX   #
# Version V3.7.0 09.01.2022                                               #
#                                                                         #
# (C) Copyright Daniel Nashed/NashCom 2005-2022                           #
# Feedback domino_unix@nashcom.de                                         #
#                                                                         #
# Licensed under the Apache License, Version 2.0 (the "License");         #
# you may not use this file except in compliance with the License.        #
# You may obtain a copy of the License at                                 #
#                                                                         #
#      http://www.apache.org/licenses/LICENSE-2.0                         #
#                                                                         #
# Unless required by applicable law or agreed to in writing, software     #
# distributed under the License is distributed on an "AS IS" BASIS,       #
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.#
# See the License for the specific language governing permissions and     #
# limitations under the License.                                          #
###########################################################################

DOMINO_SCRIPT_VERSION=3.7.0

VERBOSE_INFO="yes"

# Get Linux version and platform
LINUX_VERSION=$(cat /etc/os-release | grep "VERSION_ID="| cut -d= -f2 | xargs)
LINUX_PRETTY_NAME=$(cat /etc/os-release | grep "PRETTY_NAME="| cut -d= -f2 | xargs)
LINUX_ID=$(cat /etc/os-release | grep "^ID="| cut -d= -f2 | xargs)

# check if inside docker 
if [ -e "/.dockerenv" ]; then
  DOCKER_ENV=yes
fi

# other container run-time
if [ -e "/run/.containerenv" ]; then
  DOCKER_ENV=yes
fi

# K8s environment
if [ -n "$KUBERNETES_SERVICE_HOST" ]; then
  DOCKER_ENV=yes
fi

if [ -z "$LOGNAME" ]; then
  if [ "$DOCKER_ENV"="yes" ]; then

    # in Docker environments check LOGNAME first. If not present use whoami, which could also fail 
    # some environments use --user to map a name, which isn't listed in the container
    # in that case use the UID

    LOGNAME=`whoami 2>/dev/null`

    if [ -z "$LOGNAME" ]; then
      LOGNAME=`id -u`
    fi

    export LOGNAME
  fi
fi

# Determine Notes/OS level user
DOMINO_USER=$LOGNAME

if [ -z "$DOMINO_USER" ]; then
  echo "Empty login name. Please check your configuration"
  exit 1
fi

# Reading global and specifiy configuration if present
DOMINO_CONFIG_FILE=/etc/sysconfig/rc_domino_config_$DOMINO_USER
DOMINO_DEFAULT_CONFIG_FILE=/etc/sysconfig/rc_domino_config

if [ -r "$DOMINO_DEFAULT_CONFIG_FILE" ]; then
  . $DOMINO_DEFAULT_CONFIG_FILE

  if [ "$VERBOSE_INFO" = "yes" ]; then
    echo "Using Domino config File [$DOMINO_DEFAULT_CONFIG_FILE]"
  fi
fi

# Include server specific config file if a exists and is readable
if [ -r "$DOMINO_CONFIG_FILE" ]; then
  . $DOMINO_CONFIG_FILE
  
  if [ "$VERBOSE_INFO" = "yes" ]; then
    echo "Using Domino config File [$DOMINO_CONFIG_FILE]"
  fi
fi

# Reading environment variable file (variables defined in config file overwrirte environment)
if [ -r "$DOMINO_ENV_FILE" ]; then
  . $DOMINO_ENV_FILE
  if [ "$VERBOSE_INFO" = "yes" ]; then
    echo "Using environment File [$DOMINO_ENV_FILE]"
  fi
fi

if [ -z "$DOMINO_CONFIGURED" ]; then
  # Configuraiton is either loaded from external config file /etc/sysconfig/rc_domino_config.
  # or defined in environment variables (not recommended).yyo

  echo "Domino Server for '$DOMINO_USER' not configured"
  exit 1
fi

if [ "$DOCKER_ENV" = "yes" ]; then
  # On Docker allow additional configuration file in data directory
  if [ -r "$DOMINO_DATA_PATH/rc_domino_config" ]; then
    . $DOMINO_DATA_PATH/rc_domino_config

    if [ "$VERBOSE_INFO" = "yes" ]; then
      echo "Using Domino config File on Docker [$DOMINO_DATA_PATH/rc_domino_config]"
    fi
  fi
fi

# --- Begin of Special Configuration Parameters ---

# The following option allows you to ensure a certain owner for all external scripts that can be executed
#EXECUTE_SCRIPT_CHECK_OWNER=root

# Base directory for domino server partitions
if [ -z "$DOMINO_DATA_PATH_BASE" ]; then
  DOMINO_DATA_PATH_BASE="/local/domino"
fi

if [ -z "$DOMINO_NEXTSTART_COMPACT_STATUSFILE" ]; then
  DOMINO_NEXTSTART_COMPACT_STATUSFILE=$DOMINO_DATA_PATH/domino_nextstartcompact
fi

# Enable to use the Java Server Controller
if [ -z "$DOMINO_USE_JAVA_CONTROLLER" ]; then
  DOMINO_USE_JAVA_CONTROLLER="no"
fi

if [ -z "$DOMINO_BROADCAST_SHUTDOWN_MESSAGE" ]; then
  DOMINO_BROADCAST_SHUTDOWN_MESSAGE="no"
fi

# Compress command used e.g. for log file compression
if [ -z "$COMPRESS_COMMAND" ]; then
  COMPRESS_COMMAND="gzip --best"
fi

# Remove command used for temp file delete
if [ -z "$REMOVE_COMMAND_TEMP" ]; then
  REMOVE_COMMAND_TEMP="rm -f"
fi

# Remove command used for cleanup file delete
if [ -z "$REMOVE_COMMAND_CLEANUP" ]; then
  REMOVE_COMMAND_CLEANUP="rm -f"
fi

# Edit command
if [ -z "$EDIT_COMMAND" ]; then
  EDIT_COMMAND="vi"
fi

# Show log command
if [ -z "$SHOW_LOG_COMMAND" ]; then
  SHOW_LOG_COMMAND="vi"
fi

# Extension command descriptions
if [ -z "$EXTENSION_COMMAND_TEXT" ]; then
  EXTENSION_COMMAND_TEXT="Custom Commands"
fi

# Enable script debug output
if [ -z "$DOMINO_DEBUG_MODE" ]; then
  DOMINO_DEBUG_MODE="no"
fi

if [ -z "$DOMINO_SHUTDOWN_DELAYED_SECONDS" ]; then
  DOMINO_SHUTDOWN_DELAYED_SECONDS=20
fi

if [ -z "$DOMINO_TIKA_SHUTDOWN_TERM_SECONDS" ]; then
  DOMINO_TIKA_SHUTDOWN_TERM_SECONDS=30
fi


# -- init.d service name used for configuration --

if [ -z "$DOMINO_RC_NAME" ]; then
  DOMINO_RC_NAME=rc_domino
fi

# -- systemd service name used for configuration --

if [ -z "$DOMINO_SYSTEMD_NAME" ]; then
  DOMINO_SYSTEMD_NAME=domino.service
fi

# -- by default use server -c ... to send server command  --
if [ -z "$DOMINO_CONSOLE_SERVERC" ]; then
  DOMINO_CONSOLE_SERVERC="yes"
fi

# --- End of special configuration parameters ---

# Determine architecture and platform
if [ `uname` = AIX ]; then
  LARCH=ibmpow
  PLATFORM_NAME=AIX
elif [ `uname` = Linux ]; then
  LARCH=linux
  UNAME=`uname -m`
  if [ $UNAME = s390 ]; then
    PLATFORM_NAME=zLinux
  elif [ $UNAME = s390x ]; then
    PLATFORM_NAME=zLinux
  else
    PLATFORM_NAME=xLinux
  fi

  # Query linux distribution
  if [ ${LARCH} = "linux" ]; then
    if [ -e /etc/SuSE-release ]; then
      LINUX_DISTRIBUTION=suse
    elif [ -e /etc/redhat-release ]; then
      LINUX_DISTRIBUTION=redhat
    else
      LINUX_DISTRIBUTION=unknown
    fi
  fi

fi

if [ -z "$LARCH" ]; then
  echo "Unknown platform `uname`"
  exit 1
fi

TAIL_CMD=tail

if [ ${LARCH} = "sunspa" ]; then
  # Solaris has a separate directory containing POSIX command line tools
  TAIL_CMD=/usr/xpg4/bin/tail
fi


# --- Default configuration begin ---

# Domino shutdown timeout in seconds
if [ -z "$DOMINO_SHUTDOWN_TIMEOUT" ]; then
  DOMINO_SHUTDOWN_TIMEOUT=600
fi

# Set Domino binary directory if not yet set
if [ -z "$LOTUS" ]; then

  if [ -x /opt/hcl/domino/bin/server ]; then
    LOTUS=/opt/hcl/domino
  elif [ -x /opt/ibm/domino/bin/server ]; then
    LOTUS=/opt/ibm/domino
  else
    LOTUS=/opt/hcl/domino
  fi
fi

# --- Default configuration end ---

# Additional internal script variables

SCRIPT_NAME=$0
SCRIPT_DIR_NAME=$(dirname $SCRIPT_NAME)
PARAM=$1
PARAM1=$1
PARAM2=$2
PARAM3=$3
PARAM4=$4
PARAM5=$5
PARAM6=$6

DOMINO_BIN=$LOTUS/bin/server
DOMINO_FIXUP_BIN=$LOTUS/bin/fixup
PS_COMMAND="ps"

if [ -z "$DOMINO_COMPACT_TASK" ]; then
  DOMINO_COMPACT_TASK=compact
fi

DOMINO_COMPACT_BIN=$LOTUS/bin/$DOMINO_COMPACT_TASK

if [ "$DOMINO_DEBUG_MODE" = "yes" ]; then
  NSD_BIN="$LOTUS/bin/nsd -debug"
else
  NSD_BIN=$LOTUS/bin/nsd
fi

if [ -z "$DOMINO_INSDIR" ]; then
  DOMINO_INSDIR=$LOTUS
fi

# Set notes exec directory for domino c-api add-on software
Notes_ExecDirectory=$LOTUS/notes/latest/$LARCH

# Used to check for running processes
LOTUS_BIN_DIR=$LOTUS/notes

# Export lib search path per platform
if [ ${LARCH} = "ibmpow" ]; then
  LIBPATH=$Notes_ExecDirectory:$LIBPATH
  export LIBPATH
fi

if [ ${LARCH} = "sunspa" ]; then
  LD_LIBRARY_PATH=$Notes_ExecDirectory:$LD_LIBRARY_PATH
  export LD_LIBRARY_PATH
fi

if [ ${LARCH} = "linux" ]; then
  # include ST directories
  LD_LIBRARY_PATH=$Notes_ExecDirectory:$LD_LIBRARY_PATH:$Notes_ExecDirectory/STOpenSSL:$Notes_ExecDirectory/sticc
  export LD_LIBRARY_PATH
fi

if [ ${LARCH} = "linux" ]; then
  PS_COMMAND="ps -w"
fi

# sanity check for compact task. dbmt might not have a start link
if [ ! -x $DOMINO_COMPACT_BIN ]; then
  DOMINO_COMPACT_BIN=$Notes_ExecDirectory/$DOMINO_COMPACT_TASK
fi

# Check if data path has been already set. else set it based on user-name and base directory.
if [ -z "$DOMINO_DATA_PATH" ]; then
  DOMINO_DATA_PATH=$DOMINO_DATA_PATH_BASE/$DOMINO_USER/notesdata # path for server data directory
fi

# General environment parameters (if defaults are not set)

if [ -z "$DOMINO_SERVER" ]; then
  DOMINO_SERVER=$DOMINO_USER # Name of Domino Server
fi

# Set Domino PID File if not present
if [ -z "$DOMINO_PID_FILE" ]; then

  # -- Domino PID file per Partition which has to match the PIDFile setting in the "domino.service" --

  DOMINO_PID_FILE=/tmp/domino.pid
fi

DOMINO_INI_PATH=$DOMINO_DATA_PATH/notes.ini
PATH=$DOMINO_DATA_PATH:$LOTUS/bin:$Notes_ExecDirectory:$Notes_ExecDirectory/res/C:$PATH 

# Check if log path has been already set. else set it based on user-name and base directory.

if [ -z "$DOMINO_LOG_DIR" ]; then
  DOMINO_LOG_DIR=$DOMINO_DATA_PATH
fi

if [ -z "$DOMINO_OUTPUT_LOG" ]; then
  DOMINO_OUTPUT_LOG=$DOMINO_LOG_DIR/$DOMINO_SERVER.log # Output file for server console
fi

if [ -z "$DOMINO_INPUT_FILE" ]; then
  DOMINO_INPUT_FILE=$DOMINO_LOG_DIR/$DOMINO_SERVER.input # Input file for server console
fi

if [ "$DOMINO_USE_JAVA_CONTROLLER" = "yes" ]; then
  JAVA_CONTROLLER_STRING="-jc -c"
fi


# Export all variables that might be useful for other scripts

export LOTUS
export LARCH
export PLATFORM_NAME
export Notes_ExecDirectory
export PATH

export DOMINO_SERVER
export DOMINO_DATA_PATH
export DOMINO_USER
export DOMINO_INI_PATH
export DOMINO_OUTPUT_LOG
export DOMINO_INPUT_FILE
export DOMINO_LOG_PATH
export DOMINO_LOG_DIR
export DOMINO_LOG_BACKUP_DIR

export DOMINO_SHUTDOWN_TIMEOUT
export DOMINO_INSDIR
export DOMINO_VIEW_REBUILD_DIR
export DOMINO_TEMP_DIR
export DOMINO_SCRIPT_DIR
export DOMINO_CUSTOM_COMMAND_BASEPATH
export DOMINO_MONITOR_FILTER_STRING
export DOMINO_DEBUG_MODE
export DOMINO_SCRIPT_VERSION
export DOMINO_3RD_PARTY_BIN_DIRS

export DOMINO_LOG_CLEAR_DAYS
export DOMINO_LOG_BACKUP_CLEAR_DAYS
export DOMINO_CUSTOM_LOG_CLEAR_DAYS
export DOMINO_LOG_DB_DAYS
export DOMINO_LOG_DB_BACKUP
export DOMINO_LOG_DB_BACKUP_DIR
export DOMINO_DOMLOG_DB_DAYS
export DOMINO_DOMLOG_DB_BACKUP
export DOMINO_DOMLOG_DB_BACKUP_DIR


# -- Standard Compact Options Domino --
# Some Options are only available in D9 and late 8.5.3 versions
# -# nn  Execute up to nn compactions in parallel using different threads. Up to a maximum of 20 Threads with a default of 1.
# -W nn  Only compact databases which haven't been compacted successfully in last nn days. Ordered by last compact time.

#DOMINO_COMPACT_TASK="compact"
#DOMINO_COMPACT_OPTIONS="-#4 -C systemdbs.ind"
#DOMINO_START_COMPACT_OPTIONS="-#4 -W7 -C systemdbs.ind"

#DOMINO_LOG_COMPACT_OPTIONS="-C log.nsf"
#DOMINO_LOG_START_COMPACT_OPTIONS="-C -W7 log.nsf"

# -- Fixup Options --
#DOMINO_START_FIXUP_OPTIONS="-f -j systemdbs.ind" 
#DOMINO_FIXUP_OPTIONS="-f -j systemdbs.ind" 

# -- DBMT Compact Options Domino 9.x --
#DOMINO_COMPACT_TASK="dbmt"
#DOMINO_COMPACT_OPTIONS="-compactThreads 4 -updallThreads 0 systemdbs.ind"
#DOMINO_START_COMPACT_OPTIONS="-compactThreads 4 -updallThreads 0 systemdbs.ind"

#DOMINO_LOG_COMPACT_OPTIONS="-compactThreads 1 log.nsf"
#DOMINO_LOG_START_COMPACT_OPTIONS="-compactThreads 1 log.nsf"

# -- Custom Command Base Path --
#DOMINO_CUSTOM_COMMAND_BASEPATH="$LOTUS/commands"

# -- Monitor Filter String --
#DOMINO_MONITOR_FILTER_STRING="Opened session for|Closed session for"

# Special Parameters

# TN 1249226 / SPR DMAT6DTTHH / D7.0.x In certain cases, running NSD manually can cause a Domino server to crash on Unix 3systems.
# This crash can occur when a process such as the Agent Manager (AMGR) or Web Server (HTTP) is executing Java code. 
# This parameter prevents the crash.
AMQ_NO_SIGWAIT_SIGTRAP=1
export AMQ_NO_SIGWAIT_SIGTRAP

# RC routines for older SuSE versions that need rc their routines for init.d and dummy functions for other platforms

if [ "$LINUX_DISTRIBUTION" = "suse" ] && [ -z "$DOMINO_SYSTEMD_NAME" ]; then
  # init suse rc
  . /etc/rc.status
else

  # Dummy functions for non-SuSE platforms and also for SLES with systemd

  rc_status ()
  {
    return 0
  }

  rc_check ()
  {
    return 0
  }

  rc_exit ()
  {
    exit $_rc_status_all
  }

  rc_failed ()
  {
    _rc_status_all=$1
    return 0
  }

  rc_reset ()
  {
    _rc_status_all=0
    return 0
  }

fi

# Reset status of this service
rc_reset


# --- Helper functions ---

DebugText ()
{
  if [ "$DOMINO_DEBUG_MODE" = "yes" ]; then

    if [ -z "$DOMINO_DEBUG_FILE" ]; then
      echo "$(date '+%F %T') Debug:" $@
    else
      echo "$(date '+%F %T') Debug:" $@ >> $DOMINO_DEBUG_FILE
    fi

  fi 

  return 0
}


errmsg ()
{
  echo $1 >&2

  return 0
}


errmsg_and_exit ()
{
  errmsg "$1"
   rc_failed 1
   rc_status -v
   rc_exit
}

print_delim ()
{
  echo "--------------------------------------------------------------------------------"
}

nsd_check_set_posix_locale ()
{
  if [ "$NSD_SET_POSIX_LC" = "yes" ]; then
   LANG=POSIX
   export LANG
  fi

  return 0
}

stop_monitor ()
{
  DebugText "stoping_monitor for MONITOR_PID:" [$MONITOR_PID]
  kill -9 $MONITOR_PID > /dev/null 2>&1
  echo "Live Console closed."
  echo
  echo
  exit 0
}

remove_file ()
{
  if [ -z "$1" ]; then
    return 1
  fi

  if [ ! -e "$1" ]; then
    return 2
  fi

  rm -rf "$1"
  return 0
}

remove_tempfiles_default ()
{
  if [ ! -z "$DOMINO_DATA_PATH" ]; then
    if [ -w "$DOMINO_DATA_PATH" ]; then
      echo "Removing tempfiles:" [$DOMINO_DATA_PATH/*.DTF]
      find $DOMINO_DATA_PATH -name "*.DTF" -exec $REMOVE_COMMAND_TEMP '{}' \;

      echo "Removing tempfiles:" [$DOMINO_DATA_PATH/*.TMP]
      find $DOMINO_DATA_PATH -type f -name "*.TMP" -exec $REMOVE_COMMAND_TEMP '{}' \;
    fi
  fi

  if [ ! -z "$DOMINO_VIEW_REBUILD_DIR" ]; then
    if [ -w "$DOMINO_VIEW_REBUILD_DIR" ]; then
      echo "Removing view rebuild tempfiles:" [$DOMINO_VIEW_REBUILD_DIR/*.DTF]
      find "$DOMINO_VIEW_REBUILD_DIR" -name "*.DTF" -exec $REMOVE_COMMAND_TEMP '{}' \;
   fi
  fi

  return 0 
}

show_version ()
{
  echo "Nash!Com Linux/Unix Start Script Version ($DOMINO_SCRIPT_VERSION)"
  return 0
}

get_notes_ini_var () 
{ 
  # $1 = filename 
  # $2 = ini.variable 

  ret_ini_var="" 
  if [ -z "$1" ]; then 
    return 0 
  fi 

  if [ -z "$2" ]; then 
    return 0 
  fi 

  ret_ini_var=`awk -F '(=| =)' -v SEARCH_STR="$2" '{if (tolower($1) == tolower(SEARCH_STR)) print $2}' $1 | xargs` 
  return 0 
}

edit_config_file ()
{
  if [ -z "$1" ]; then
    if [ -r "$DOMINO_CONFIG_FILE" ]; then
      $EDIT_COMMAND "$DOMINO_CONFIG_FILE"
    else
      if [ -r "$DOMINO_DEFAULT_CONFIG_FILE" ]; then
        $EDIT_COMMAND $DOMINO_DEFAULT_CONFIG_FILE
      fi
    fi
  elif [ "$1" = "default" ]; then
    $EDIT_COMMAND "$DOMINO_DEFAULT_CONFIG_FILE"
  elif  [ "$1" = "server" ]; then
    $EDIT_COMMAND "$DOMINO_DEFAULT_CONFIG"
  elif  [ -r "$1" ]; then
    $EDIT_COMMAND "$1"
  else
    echo "Wrong config edit option [$1] specified"
  fi
}

edit_debug_file()
{
  if [ -z "$DOMINO_DEBUG_FILE" ]; then
    return 0
  fi

  if [ ! -r "$DOMINO_DEBUG_FILE" ]; then
    return 0
  fi
  
  if [ -z "$1" ]; then
    $EDIT_COMMAND "$DOMINO_DEBUG_FILE"
  else
    $1 "$DOMINO_DEBUG_FILE"
  fi
}

list_config_file ()
{
  if [ -z "$1" ]; then
    if [ -r "$DOMINO_CONFIG_FILE" ]; then
      cat "$DOMINO_CONFIG_FILE" | grep . | grep -v "^#" 
    else
      if [ -r "$DOMINO_DEFAULT_CONFIG_FILE" ]; then
        cat $DOMINO_DEFAULT_CONFIG_FILE | grep . | grep -v "^#" 
      fi
    fi
  elif [ "$1" = "default" ]; then
    if [ -r "$DOMINO_DEFAULT_CONFIG_FILE" ]; then
      cat "$DOMINO_DEFAULT_CONFIG_FILE" | grep . | grep -v "^#" 
    else
      echo "Config file not found"
    fi
  elif  [ "$1" = "server" ]; then
    if [ -r "$DOMINO_CONFIG_FILE" ]; then
      cat "$DOMINO_CONFIG_FILE" | grep . | grep -v "^#" 
    else
      echo "Config file not found"
    fi
  elif  [ -r "$1" ]; then
    cat "$1" | grep . | grep -v "^#" 
  else
    echo "Wrong config list option [$1] specified"
  fi
}

edit_systemd_cfg ()
{
  if [ ! -z "$DOMINO_SYSTEMD_NAME" ]; then
    SYSTEMD_NAME=/etc/systemd/system/$DOMINO_SYSTEMD_NAME
  	
    if [ -r "$SYSTEMD_NAME" ]; then
      $EDIT_COMMAND $SYSTEMD_NAME
    else
      echo "systemd not configured!"
    fi
  fi
}


show_log ()
{
  if [ -z "$1" ]; then
    CURRENT_SHOW_LOG_COMMAND=$SHOW_LOG_COMMAND
  else
    CURRENT_SHOW_LOG_COMMAND="$@"
  fi

  $CURRENT_SHOW_LOG_COMMAND $REAL_OUTPUT_FILE
}

run_external_script ()
{
  if [ -z "$1" ]; then
    return 0
  fi
  
  if [ ! -x $1 ]; then
    echo "Cannot execute script " [$1]
    return 0
  fi
  
  if [ -z "$EXECUTE_SCRIPT_CHECK_OWNER" ]; then
    DebugText "not checking script owner for [$1]" 
  else
    SCRIPT_OWNER=`stat -c %U $1`  
    if [ "$SCRIPT_OWNER" = "$EXECUTE_SCRIPT_CHECK_OWNER" ]; then
      DebugText "execution check script [$1] OK -- Owner: $SCRIPT_OWNER" 
    else
      echo "Wrong owner for script -- not executing" [$1] 
      return 0
    fi
  fi
  
  DebugText "BEGIN $2 " [$1]
  $1
  DebugText "END $2 " [$1]
  
  return 0
}

remove_tempfile_check ()
{
  if [ "$DOMINO_REMOVE_TEMPFILES" = "yes" ]; then

    if [ -z "$DOMINO_CUSTOM_REMOVE_TEMPFILES_SCRIPT" ]; then
      remove_tempfiles_default
    else
      run_external_script "$DOMINO_CUSTOM_REMOVE_TEMPFILES_SCRIPT" "custom remove tempfiles script"
    fi
  fi
}

clear_backup_logs ()
{
  DebugText "clear_backup_logs PARAM: '$1' '$2'"
  if [ -z "$1" ]; then
 
    if [ -z "$DOMINO_LOG_BACKUP_CLEAR_DAYS" ]; then
      return 0
    fi
 
    DebugText "log clear days from config:" $DOMINO_LOG_BACKUP_CLEAR_DAYS
 
  else
    DOMINO_LOG_BACKUP_CLEAR_DAYS=$1  
    DebugText "log clear days from parameter:" $DOMINO_LOG_BACKUP_CLEAR_DAYS
  fi

  echo "Removing log backup files - days: $DOMINO_LOG_BACKUP_CLEAR_DAYS"

  if [ -z "$DOMINO_LOG_BACKUP_DIR" ]; then
    if [ -w "$DOMINO_DATA_PATH" ]; then
      DebugText "Removing old log backup files from : $DOMINO_DATA_PATH"
      echo "Removing old log-files from data dir: [$DOMINO_DATA_PATH/$DOMINO_SERVER_*.log.gz - days: $DOMINO_LOG_BACKUP_CLEAR_DAYS]"
      find "$DOMINO_DATA_PATH" -maxdepth 1 -type f -mtime +$DOMINO_LOG_BACKUP_CLEAR_DAYS -name "$DOMINO_SERVER_*.log.gz" -exec $REMOVE_COMMAND_CLEANUP {} \; 
    fi
  else
    if [ -w "$DOMINO_LOG_BACKUP_DIR" ]; then
      DebugText "Removing files from : $DOMINO_LOG_BACKUP_DIR"
      echo "Removing log-files from backup dir: [$DOMINO_LOG_BACKUP_DIR/$DOMINO_SERVER_*.log.gz - days: $DOMINO_LOG_BACKUP_CLEAR_DAYS]"
      find "$DOMINO_LOG_BACKUP_DIR" -maxdepth 1 -type f -mtime +$DOMINO_LOG_BACKUP_CLEAR_DAYS -name "$DOMINO_SERVER_*.log.gz" -exec $REMOVE_COMMAND_CLEANUP {} \; 
    fi
  fi
}

clear_logs ()
{
  DebugText "clear_logs PARAM: '$1' '$2'"
  
  if [ -z "$1" ]; then
 
    if [ -z "$DOMINO_LOG_CLEAR_DAYS" ]; then
      return 0
    fi
 
    DebugText "log clear days from config: $DOMINO_LOG_CLEAR_DAYS"
 
  else
    DOMINO_LOG_CLEAR_DAYS=$1  
    DebugText "log clear days from parameter: $DOMINO_LOG_CLEAR_DAYS"
  fi

  echo "Removing log files - days: $DOMINO_LOG_CLEAR_DAYS"

  if [ ! -z "$DOMINO_LOG_PATH" ]; then
    if [ -w "$DOMINO_LOG_PATH" ]; then
      DebugText "Removing files from : $DOMINO_LOG_PATH"
      echo "Removing log-files: [$DOMINO_LOG_PATH/* - days: $DOMINO_LOG_CLEAR_DAYS]"
      find "$DOMINO_LOG_PATH" -type f -mtime +$DOMINO_LOG_CLEAR_DAYS -exec $REMOVE_COMMAND_CLEANUP {} \; 
    fi
  fi

  if [ ! -z "$DOMINO_IBM_TECHNICAL_SUPPORT_DIR" ]; then
    if [ -w "$DOMINO_IBM_TECHNICAL_SUPPORT_DIR" ]; then
      DebugText "Removing files from : $DOMINO_IBM_TECHNICAL_SUPPORT_DIR"
      echo "Removing log-files: [$DOMINO_IBM_TECHNICAL_SUPPORT_DIR/* - days: $DOMINO_LOG_CLEAR_DAYS]"
      find "$DOMINO_IBM_TECHNICAL_SUPPORT_DIR" -type f -mtime +$DOMINO_LOG_CLEAR_DAYS -exec $REMOVE_COMMAND_CLEANUP {} \; 
    fi
  fi

  return 0
}

clear_custom_logs ()
{
  DebugText "clear_custom_logs PARAM: '$1' '$2'"

  if [ -z "$1" ]; then
 
    if [ -z "$DOMINO_CUSTOM_LOG_CLEAR_DAYS" ]; then
      return 0
    fi
 
    DebugText "custom-log clear days from config: $DOMINO_CUSTOM_LOG_CLEAR_DAYS"
 
  else
    DOMINO_CUSTOM_LOG_CLEAR_DAYS=$1
    DebugText "custom-log clear days from parameter: $DOMINO_CUSTOM_LOG_CLEAR_DAYS"
  fi

  if [ -z "$2" ]; then
 
    if [ -z "$DOMINO_CUSTOM_LOG_CLEAR_PATH" ]; then
      echo "No custom-log clear path specified"
      return 0
    fi
 
    DebugText "custom-log clear path from config: $DOMINO_CUSTOM_LOG_CLEAR_PATH"
 
  else
    DOMINO_CUSTOM_LOG_CLEAR_PATH=$2
    DebugText "custom-log clear path from parameter: $DOMINO_CUSTOM_LOG_CLEAR_PATH"
  fi

  if [ ! -z "$DOMINO_CUSTOM_LOG_CLEAR_PATH" ]; then
    if [ -w "$DOMINO_CUSTOM_LOG_CLEAR_PATH" ]; then

      DebugText "Removing custom-log files - days: $DOMINO_CUSTOM_LOG_CLEAR_DAYS path: $DOMINO_CUSTOM_LOG_CLEAR_PATH"
      echo "Removing log-files: [$DOMINO_CUSTOM_LOG_CLEAR_PATH - days: $DOMINO_CUSTOM_LOG_CLEAR_DAYS]"
      find "$DOMINO_CUSTOM_LOG_CLEAR_PATH" -type f -mtime +$DOMINO_CUSTOM_LOG_CLEAR_DAYS -exec $REMOVE_COMMAND_CLEANUP {} \; 
    fi
  fi
 
  return 0
}

log_clear_operations ()
{
  if [ -z "$DOMINO_CUSTOM_LOG_CLEAR_SCRIPT" ]; then
    clear_logs "$1"
    clear_custom_logs "$1"
    clear_backup_logs "$2"
  else
    run_external_script "$DOMINO_CUSTOM_LOG_CLEAR_SCRIPT" "custom log clear script"
  fi
   
  return 0
}

startup_remove_file_check ()
{
  DOMINO_LOADMON_FILE=$DOMINO_DATA_PATH/loadmon.ncf

  DebugText "DOMINO_LOADMON_FILE:" [$DOMINO_LOADMON_FILE]

  if [ "$DOMINO_RESET_LOADMON" = "yes" ]; then
    if [ -w $DOMINO_LOADMON_FILE ]; then
      rm $DOMINO_LOADMON_FILE
      
      if [ $? -eq 0 ]; then
        echo "Removed LoadMon-Data '$DOMINO_LOADMON_FILE'"
      else
        echo "Cannot remove LoadMon-Data '$DOMINO_LOADMON_FILE'"
      fi
     
    fi
  fi


  if [ "$DOMINO_CLEAR_LOGS_STARTUP" = "yes" ]; then
    log_clear_operations
  fi

  remove_tempfile_check

  return 0
}

check_create_dir ()
{
  if [ -z "$1" ]; then
    return 0
  fi

  if [ -e "$1" ]; then
    return 0
  else
    mkdir -p "$1"
    if [ -w "$1" ]; then
      echo "Successfully created $2 ($1)"
      return 0
    else
      echo "Error creating $2 ($1)"
      return 1
    fi
  fi

  return 0
}

check_create_directories ()
{
  DebugText "check create for DOMINO_VIEW_REBUILD_DIR" [$DOMINO_VIEW_REBUILD_DIR]
  check_create_dir "$DOMINO_VIEW_REBUILD_DIR" "View Rebuild Dir"

  DebugText "check create for DOMINO_TEMP_DIR" [$DOMINO_TEMP_DIR]
  check_create_dir "$DOMINO_TEMP_DIR" "Domino Temp Path"

  DebugText "check create for DOMINO_LOG_PATH" [$DOMINO_LOG_PATH]
  check_create_dir "$DOMINO_LOG_PATH" "Domino Log Path"

  DebugText "check create for DOMINO_LOG_DIR" [$DOMINO_LOG_DIR]
  check_create_dir "$DOMINO_LOG_DIR" "Domino Log Dir"
  
  DebugText "check create for DOMINO_LOG_BACKUP_DIR" [$DOMINO_LOG_BACKUP_DIR]
  check_create_dir "$DOMINO_LOG_BACKUP_DIR" "Domino Log Backup Dir"

  return 0
}

check_log_db_age()
{
  if [ -z "$DOMINO_LAST_LOG_DB_FILE" ]; then
    DOMINO_LAST_LOG_DB_FILE="$DOMINO_DATA_PATH/domino_last_log_db.txt"
  fi

  if [ -z "$DOMINO_LOG_DB_DAYS" ]; then
    DebugText "No DOMINO_LOG_DB_DAYS specified"
    return 0
  fi

  if [ ! -r "$DOMINO_LAST_LOG_DB_FILE" ]; then
    DebugText "$DOMINO_LAST_LOG_DB_FILE not present -> first run"
    LANG=C date > $DOMINO_LAST_LOG_DB_FILE
    return 1
  fi

  last_log_db_date=`cat $DOMINO_LAST_LOG_DB_FILE`
  DebugText "last_log_db_date: $last_log_db_date"

  if [ -z "$last_log_db_date" ]; then
    LANG=C date > $DOMINO_LAST_LOG_DB_FILE
    DebugText "Empty status file -> first run"
    return 1
  fi

  d1=$(LANG=C date -d "now" +%s)
  d2=$(LANG=C date -d "$last_log_db_date" +%s)
  diff_days=$(( (d1 - d2) / 86400 ))

  DebugText "days since last log rename: $diff_days"

  if [ $diff_days -ge $DOMINO_LOG_DB_DAYS ]; then
    DebugText "Sufficient Days leapsed -> renaming log"
    LANG=C date > $DOMINO_LAST_LOG_DB_FILE
    return 1
  fi

  return 0
}

check_domlog_db_age()
{
  if [ -z "$DOMINO_LAST_DOMLOG_DB_FILE" ]; then
    DOMINO_LAST_DOMLOG_DB_FILE="$DOMINO_DATA_PATH/domino_last_domlog_db.txt"
  fi

  if [ -z "$DOMINO_DOMLOG_DB_DAYS" ]; then
    DebugText "No DOMINO_DOMLOG_DB_DAYS specified"
    return 0
  fi

  if [ ! -r "$DOMINO_LAST_DOMLOG_DB_FILE" ]; then
    DebugText "$DOMINO_LAST_DOMLOG_DB_FILE not present -> first run"
    LANG=C date > $DOMINO_LAST_DOMLOG_DB_FILE
    return 1
  fi

  last_domlog_db_date=`cat $DOMINO_LAST_DOMLOG_DB_FILE`
  DebugText "last_domlog_db_date: $last_domlog_db_date"

  if [ -z "$last_domlog_db_date" ]; then
    LANG=C date > $DOMINO_LAST_DOMLOG_DB_FILE
    DebugText "Empty status file -> first run"
    return 1
  fi

  d1=$(LANG=C date -d "now" +%s)
  d2=$(LANG=C date -d "$last_domlog_db_date" +%s)
  diff_days=$(( (d1 - d2) / 86400 ))

  DebugText "days since last domlog rename: $diff_days"

  if [ $diff_days -ge $DOMINO_DOMLOG_DB_DAYS ]; then
    DebugText "Sufficient Days leapsed -> renaming domlog"
    LANG=C date > $DOMINO_LAST_DOMLOG_DB_FILE
    return 1
  fi

  return 0
}

move_database()
{
  DebugText "Moving [$1] to [$2]"

  if [ -z "$1" ]; then
    return 0
  fi

  if [ -z "$2" ]; then
    return 0
  fi


  if [ "$2" = "DELETEDB" ]; then
    echo "--- Removing $1 ---" >> $DOMINO_OUTPUT_LOG
    echo "Removing $1"
  else
    echo "--- Moving $1 to $2 ---" >> $DOMINO_OUTPUT_LOG
    echo "Moving $1 to $2"

    if [ -e "$2" ]; then
      DebugText "Removing old $2"

      echo "--- Removing old $2 ---" >> $DOMINO_OUTPUT_LOG 
      echo "Removing old '$2' "

      rm -f "$2"
    fi
  fi

  if [ -e "$2" ]; then
    echo "--- Error renaming log database -- Cannot remove existing file [$1 -> $2] ---" >> $DOMINO_OUTPUT_LOG 
    echo "Error renaming log database -- Cannot remove existing file [$1 -> $2]"
    return 1
  fi

  directory=`dirname $2`
  DebugText "dirname for rename [$directory]"
  check_create_dir "$directory" "Database Log Dir"
  
  if [ ! -w "$directory" ]; then
    echo "--- Error renaming log database. Cannot write to directory '$directory' [$1 -> $2] ---" >> $DOMINO_OUTPUT_LOG 
    echo "Error renaming log database. Cannot write to directory '$directory' [$1 -> $2]"
    return 1
  fi 

  if [ "$2" = "DELETEDB" ]; then
    rm -f "$1"
  else
    mv "$1" "$2"
  fi

  if [ -e "$1" ]; then
    echo "--- Error renaming log database [$1 -> $2] ---" >> $DOMINO_OUTPUT_LOG 
    echo "Error renaming log database [$1 -> $2]"
    return 1
  fi
   
  return 0
}

check_log_db_rename()
{
  if [ -z "$DOMINO_LOG_DB_DAYS" ]; then
    DebugText "No DOMINO_LOG_DB_DAYS specified"
    return 0
  fi

  # check age anyway to set timestamp - even file might not exist
  check_log_db_age

  if [ "$?" = "1" ]; then

    DOMINO_LOG_DB=$DOMINO_DATA_PATH/log.nsf

    if [ ! -e "$DOMINO_LOG_DB" ]; then
      DebugText "No log.nsf - Skip move"
      return 0
    fi
    
    if [ -z "$DOMINO_LOG_DB_BACKUP" ]; then

      if [ -z "$DOMINO_LOG_DB_BACKUP_DIR" ]; then
        DOMINO_LOG_DB_BACKUP_DIR=$DOMINO_DATA_PATH/log_backup
      fi

      check_create_dir "$DOMINO_LOG_DB_BACKUP_DIR" "Domino LogDbBackup Dir"

      DOMINO_LOG_DB_BACKUP="$DOMINO_LOG_DB_BACKUP_DIR/log_"`LANG=C date +"%Y%m%d"`".nsf"
    else
      # create directory anyway if configured. log file might need it
      check_create_dir "$DOMINO_LOG_DB_BACKUP_DIR" "Domino LogDbBackup Dir"
    fi

    move_database $DOMINO_LOG_DB $DOMINO_LOG_DB_BACKUP
  fi

  return 0
}

check_domlog_db_rename()
{
  if [ -z "$DOMINO_DOMLOG_DB_DAYS" ]; then
    DebugText "No DOMINO_DOMLOG_DB_DAYS specified"
    return 0
  fi

  # check age anyway to set timestamp - even file might not exist
  check_domlog_db_age

  if [ "$?" = "1" ]; then

    DOMINO_DOMLOG_DB=$DOMINO_DATA_PATH/domlog.nsf

    if [ ! -e "$DOMINO_DOMLOG_DB" ]; then
      DebugText "No domlog.nsf - Skip move"
      return 0
    fi
    
    if [ -z "$DOMINO_DOMLOG_DB_BACKUP" ]; then
      
      if [ -z "$DOMINO_DOMLOG_DB_BACKUP_DIR" ]; then
        DOMINO_DOMLOG_DB_BACKUP_DIR=$DOMINO_DATA_PATH/log_backup
      fi

      check_create_dir "$DOMINO_DOMLOG_DB_BACKUP_DIR" "Domino LogDbBackup Dir"
      
      DOMINO_DOMLOG_DB_BACKUP="$DOMINO_DOMLOG_DB_BACKUP_DIR/log_"`LANG=C date +"%Y%m%d"`".nsf"
    else
      # create directory anyway if configured. log file might need it
      check_create_dir "$DOMINO_DOMLOG_DB_BACKUP_DIR" "Domino LogDbBackup Dir"
    fi
   
    move_database $DOMINO_DOMLOG_DB $DOMINO_DOMLOG_DB_BACKUP
  fi

  return 0
}

domino_is_running ()
{
  DOMINO_RUNNING=`$PS_COMMAND -fu $DOMINO_USER | grep "$LOTUS_BIN_DIR" | grep "server" | grep -v " -jc"`

  DebugText "DOMINO_USER:" $DOMINO_USER 
  DebugText "DOMINO_RUNNING:" $DOMINO_RUNNING 

  if [ -n "$DOMINO_RUNNING" ]; then
    DebugText "Domino is running"
    return 1
  else
    DebugText "Domino is NOT running"
    return 0
  fi

  return 0
}

get_tika_status ()
{
  TIKA_PROCESS=`$PS_COMMAND -fu $DOMINO_USER | grep "$Notes_ExecDirectory/jvm/bin/java" | grep "tika-server.jar" | awk '{ print $2 }'`
  DebugText "Tika-Process: [$TIKA_PROCESS]"
}

tika_server ()
{
  get_tika_status

  case "$1" in

    stop)

     if [ -z "$TIKA_PROCESS" ]; then
        DebugText "Tika process not running (stop)"
      else
        echo "Stopping Tika processes"

        for pid in `$PS_COMMAND -fu $DOMINO_USER | grep "$Notes_ExecDirectory/jvm/bin/java" | grep "tika-server.jar" | awk '{ print $2 }'`; do
          echo "Killing Tika pid #" $pid
          kill -s 15 $pid
        done
      fi
      ;;

    kill)
 
      if [ -z "$TIKA_PROCESS" ]; then
        DebugText "Tika process not running (kill)"
      else
        echo "Terminating Tika processes"

        for pid in `$PS_COMMAND -fu $DOMINO_USER | grep "$Notes_ExecDirectory/jvm/bin/java" | grep "tika-server.jar" | awk '{ print $2 }'`; do
          echo "Killing Tika pid #" $pid
          kill -9 $pid
        done
      fi 
      ;;

     "")
      if [ -z "$TIKA_PROCESS" ]; then
        echo "Tika Server NOT running"
      else
        echo "Tika Server running"
      fi
      ;;
    *)
      echo "Unknown tika command option '$1'"
      ;;
    esac
}

check_shutdown_delayed_commands()
{
  if [ ! -z "$DOMINO_TIKA_SHUTDOWN_TERM_SECONDS" ]; then
    if [ "$1" -ge "$DOMINO_TIKA_SHUTDOWN_TERM_SECONDS" ]; then
      tika_server kill 
    fi
  fi

  if [ ! -z "$DOMINO_SHUTDOWN_DELAYED_SCRIPT" ]; then
    if [ -z "$SHUTDOWN_DELAY_CHECKED" ]; then
      if [ "$1" -ge "$DOMINO_SHUTDOWN_DELAYED_SECONDS" ]; then
        DebugText "Invoking ShutDownDleayedScript [$DOMINO_SHUTDOWN_DELAYED_SCRIPT]"
        if [ -x "$DOMINO_SHUTDOWN_DELAYED_SCRIPT" ]; then
          $DOMINO_SHUTDOWN_DELAYED_SCRIPT
        else
          echo "Cannot execute shutdown delayed script [$DOMINO_SHUTDOWN_DELAYED_SCRIPT]"
        fi
        SHUTDOWN_DELAY_CHECKED=1
      fi
    fi
  fi
}

domino_status_check ()
{
  domino_is_running
  if [ $? -eq 1 ]; then
 
    if [ "$1" = "display" ]; then
      echo "[ ON  ] - $DOMINO_SERVER"
    else
      echo
      echo "Domino Server is running ($DOMINO_SERVER)"
      echo
    fi
    return 0
  else
    if [ "$1" = "display" ]; then
      echo "[ OFF ] - $DOMINO_SERVER"
    else
      echo
      echo "Domino Server is NOT running ($DOMINO_SERVER)"
      echo
    fi
    return 3
  fi

  return 0
}

java_controller_is_running ()
{
  JAVA_CONTROLLER_RUNNING=`$PS_COMMAND -fu $DOMINO_USER | grep "$LOTUS_BIN_DIR" | grep " -jc" | grep -v "grep"`

  if [ -n "$JAVA_CONTROLLER_RUNNING" ]; then
    DebugText "Java Controller is running"
    return 1
  else
    DebugText "Java Controller is NOT running"
    return 0
  fi

  return 0
}

archive_logs ()
{
  echo
  cd $DOMINO_DATA_PATH

  if [ -z "$1" ]; then
    LOG_NAME_INFO=""
  else
    LOG_NAME_INFO="_"$1
  fi
  
  # Move log-file and compress it
  LOG_NAME=`echo "$DOMINO_OUTPUT_LOG" |cut -d. -f1`
  LOG_EXTENSION=`echo "$DOMINO_OUTPUT_LOG" |cut -d. -f2`

  #sanity check for log name
  if [ -z "$LOG_NAME" ]; then
    LOG_NAME=$DOMINO_OUTPUT_LOG
    LOG_EXTENSION="log"
  fi

  DebugText "LOG_NAME: '$LOG_NAME'"
  DebugText "LOG_NAME_INFO: '$LOG_NAME_INFO'"

  if [ -z "$DOMINO_LOG_BACKUP_DIR" ]; then
    DOMINO_OUTPUT_LOG_BACKUP="$LOG_NAME"_`LANG=C date +"%y%m%d_%H%M%S"`"$LOG_NAME_INFO"."$LOG_EXTENSION"
  else
    DOMINO_OUTPUT_LOG_BACKUP="$DOMINO_LOG_BACKUP_DIR"/`basename "$LOG_NAME"`_`LANG=C date +"%y%m%d_%H%M%S"`"$LOG_NAME_INFO"."$LOG_EXTENSION"
  fi

  DebugText "backup log filename:" $DOMINO_OUTPUT_LOG_BACKUP

  # Copy log-file and compress
  if [ -r $DOMINO_OUTPUT_LOG ]; then

    domino_is_running
    if [ $? -eq 1 ]; then
      # copy log file 
      cp $DOMINO_OUTPUT_LOG $DOMINO_OUTPUT_LOG_BACKUP >/dev/null
      
      # empty log file 
      cat /dev/null > $DOMINO_OUTPUT_LOG
    else
      mv $DOMINO_OUTPUT_LOG $DOMINO_OUTPUT_LOG_BACKUP
    fi

    echo "--- Moved log file to '$DOMINO_OUTPUT_LOG_BACKUP' ---" >> $DOMINO_OUTPUT_LOG
      
    # compress log file

    if [ "$DOMINO_ARCHIVE_LOG_BACKGROUND" = "yes" ]; then
      DebugText "archiving logs in background to '$DOMINO_OUTPUT_LOG_BACKUP'" 
      $COMPRESS_COMMAND $DOMINO_OUTPUT_LOG_BACKUP >/dev/null &
    else
      DebugText "archiving logs to '$DOMINO_OUTPUT_LOG_BACKUP'"
      $COMPRESS_COMMAND $DOMINO_OUTPUT_LOG_BACKUP >/dev/null
    fi

    echo "Archived log file to '$DOMINO_OUTPUT_LOG_BACKUP'"
  else
    echo "No log file to archive"
  fi

  return 0
}

pre_shutdown_operations()
{
  run_external_script "$DOMINO_PRE_SHUTDOWN_SCRIPT" "pre-shutdown-script"
  return 0
}

post_shutdown_operations()
{
  run_external_script "$DOMINO_POST_SHUTDOWN_SCRIPT" "post-shutdown-script"
  return 0
}

pre_startup_operations()
{
  run_external_script "$DOMINO_PRE_STARTUP_SCRIPT" "pre-startup-script"
  return 0
}

post_startup_operations()
{
  run_external_script "$DOMINO_POST_STARTUP_SCRIPT" "post-startup-script"
  return 0
}

pre_kill_operations()
{
  run_external_script "$DOMINO_PRE_KILL_SCRIPT" "pre-kill-script"
  return 0
}

post_kill_operations()
{
  run_external_script "$DOMINO_POST_KILL_SCRIPT" "post-kill-script"
  return 0
}

pre_cleanup_operations()
{
  run_external_script "$DOMINO_PRE_CLEANUP_SCRIPT" "pre-cleanup-script"
  return 0
}

post_cleanup_operations()
{
  run_external_script "$DOMINO_POST_CLEANUP_SCRIPT" "post-cleanup-script"
  return 0
}

pre_status_operations()
{
  run_external_script "$DOMINO_PRE_STATUS_SCRIPT" "pre-status-script"
  return 0
}

run_monitor ()
{
  if [ ! -r "$REAL_OUTPUT_FILE" ]; then
    errmsg_and_exit "Cannot read server-log ($REAL_OUTPUT_FILE) - exiting"
  fi

  # if input file does not exist, create it
  if [ ! -e "$DOMINO_INPUT_FILE" ]; then
    touch "$DOMINO_INPUT_FILE"
  fi

  if [ ! -w "$DOMINO_INPUT_FILE" ]; then
    errmsg_and_exit "Cannot write server-input-file ($DOMINO_INPUT_FILE) - exiting"
  fi
  
  echo 
  echo "--- Live Console for $DOMINO_SERVER ---"
  echo 
  echo "To close console, always type 'close' or 'stop'."
  echo 
  echo 
  sleep 2


  if [ "$1" = "filter" ]; then
    if [ -z "$DOMINO_MONITOR_FILTER_STRING" ]; then
      FILTER_STRING=""
    else
      FILTER_STRING=$DOMINO_MONITOR_FILTER_STRING
    fi
  else
    FILTER_STRING=""
  fi

  DebugText "FILTER_STRING:" [$FILTER_STRING]

  if [ -z "$FILTER_STRING" ]; then
    $TAIL_CMD -f $REAL_OUTPUT_FILE &
  else
    $TAIL_CMD -f $REAL_OUTPUT_FILE | grep -v -E "$FILTER_STRING" &
  fi

  MONITOR_PID=$!
  DebugText "MONITOR_PID:" [$MONITOR_PID]
 
  trap "stop_monitor" 1 2 3 4 6 9 13 15 19 23

  # avoid expansion of wildcards like star
  set -f 

  var=dummy
  while true
  do
    lastvar=$var
    var="PIPE-DEAD"
    read var
    
    if [ "$var" = "PIPE-DEAD" ]; then
      echo "Terminating live console - Input Pipe is dead!"
      stop_monitor
      exit 0
    fi
    
    CHARS=`echo "$var" | wc -m`
    if [ "$CHARS" -lt 5 ]; then # includes the new line -> means commands 3 chars or less
      if [ "$var" = "$lastvar" ]; then
        echo "Terminating live console - Repeated short command!"
        stop_monitor
        exit 0
      fi
    fi

    if [ "$var" = "exit" ]; then
      echo "'$var' ignored. use 'QUIT' to shutdown the server. use 'close' or 'stop' to close live console"
    elif [ "$var" = "quit" ]; then
      echo "'$var' ignored. use 'QUIT' to shutdown the server. use 'close' or 'stop' to close live console"
    elif [ "$var" = "e" ]; then
      echo "'$var' ignored. use 'QUIT' to shutdown the server. use 'close' or 'stop' to close live console"
    elif [ "$var" = "q" ]; then
      echo "'$var' ignored. use 'QUIT' to shutdown the server. use 'close' or 'stop' to close live console"
    elif [ "$var" = "stop" ]; then
      stop_monitor
      return 0
    elif [ "$var" = "close" ]; then
      stop_monitor
      return 0
    else
      if [ ! -z "$var" ]; then

        domino_is_running
        if [ $? -eq 1 ]; then
          if [ "$DOMINO_CONSOLE_SERVERC" = "yes" ]; then
            cd "$DOMINO_DATA_PATH"
            $DOMINO_BIN -c "$var"
          else
            echo "$var" >> $DOMINO_INPUT_FILE
          fi
        else
          echo "Warning: Server not running -- Command ignored"
        fi
      fi
    fi
  done

  return 0
}

check_for_crashed_server ()
{
  nsd_status=`$TAIL_CMD -n 5000 $REAL_OUTPUT_FILE |grep "NSD is in progress" | wc -l`

  if [ $nsd_status -eq 0 ]; then
    return 0
  fi

  echo
  echo "!!! SERVER CRASHED ON SHUTDOWN !!!"
  echo

  FR_HISTORY_FILE=$DOMINO_DATA_PATH"/fault_recovery.hst"
  NSD_NBF=$DOMINO_DATA_PATH"/nsdindex.nbf"

  if [ -r $FR_HISTORY_FILE ]; then
    echo "--- BEGIN - Fault Recovery History ---"
    cat $FR_HISTORY_FILE
    echo "--- END - Fault Recovery History ---"
    echo
  fi

  if [ -r $NSD_NBF ]; then
    echo "Check NSD for Details:"
    cat $NSD_NBF
    echo
  fi

  return 1
}

dump_configuration ()
{
  DebugText "DOMINO_CONFIGURED:" [$DOMINO_CONFIGURED]
  DebugText "LOTUS:" [$LOTUS]
  DebugText "DOMINO_DATA_PATH:" [$DOMINO_DATA_PATH]
  DebugText "DOMINO_LANG:" [$DOMINO_LANG]
  DebugText "LANG:" [$LANG]
  DebugText "LC_ALL:" [$LC_ALL]
  DebugText "DOMINO_SHUTDOWN_TIMEOUT:" [$DOMINO_SHUTDOWN_TIMEOUT]
  DebugText "DOMINO_REMOVE_TEMPFILES:" [$DOMINO_REMOVE_TEMPFILES]
  DebugText "DOMINO_RESET_LOADMON:" [$DOMINO_RESET_LOADMON]

  DebugText "SCRIPT_NAME:" [$SCRIPT_NAME]
  DebugText "PARAM1:" [$PARAM1]
  DebugText "PARAM2:" [$PARAM2]
  DebugText "PARAM3:" [$PARAM3]
  DebugText "PARAM4:" [$PARAM4]
  DebugText "PARAM5:" [$PARAM5]
  DebugText "PARAM6:" [$PARAM6]

  return 0
}

usage_extension_commands ()
{
  if [ ! -z "$DOMINO_CUSTOM_COMMAND_BASEPATH" ]; then
    if [ -x "$DOMINO_CUSTOM_COMMAND_BASEPATH" ]; then
      echo
      custom_commands=
      ALL_SCRIPTS=`find $DOMINO_CUSTOM_COMMAND_BASEPATH/* -maxdepth 1`
      for script_name in $ALL_SCRIPTS; do
        if [ -x "$script_name" ]; then
          custom_command=`basename $script_name`
          if [ -z "$custom_commands" ]; then
            custom_commands=$custom_command
          else
            custom_commands="$custom_commands | $custom_command"
          fi
        fi
      done

      echo "$EXTENSION_COMMAND_TEXT: $custom_commands"
    fi
  fi

  return 0
}


usage ()
{
  echo 
  show_version
  echo
  echo "Usage: `basename $SCRIPT_NAME` { start | stop | restart | status | console (monitor) | quit}"
  echo "{ start live | stop live | restart live | compact | restartcompact | compactnextstart}"
  echo "{ cmd <command> <n>}"
  echo "{ about(+) |version | help}"
  echo
  echo "Diag Commands   :  {resources/res | info | stacks  |  nsd  | nsdnomem | memdump | hang | kill | cleanup}"
  echo "Log Commands    :  {archivelog    | clearlog  | lastlog    | log | systemlog}"
  echo "Config Commands :  {inivar | ini  | listini   | config/cfg | systemdcfg}"
  echo "Setup  Commands :  {setup json    | setup env | setup      | setup log [edit] }"

  if [ -e "$DOMINO_DATA_PATH/trace" ]; then
    echo "ST Commands     :  {resetstlogs}"
  fi

  usage_extension_commands
  echo

  return 0
}

help ()
{
  usage
  echo
  echo "start            start service/server"
  echo "stop             stop  server/service"
  echo "status           check status of server"
  echo "console          invoke live console"
  echo "monitor          invoke live console"
  echo "quit             stop server but leave server controller running"
  
  echo 
  echo "start live       start server with live console"
  echo "stop live        stop server with live console"
  echo "restart live     restart server with live console"
  echo "compact          compact system databases if server is down"
  echo "restartcompact   stop server, run compact and restart server "
  echo "compactnextstart on|off|status -- enables/disables startup compact operations at next startup"
  echo "version          print start script version"
  echo "about|about+     show machine info. With 'About' or 'about+' also show info from https://ipinfo.io/"
  echo "cmd <cmd> <n>    run command server command and optinally print last <n> console log lines"
  
  echo
  echo "Diag Commands"
  echo
  echo "resources(res)   shows used server resources (processes, shared mem, MQs, semaphores)"
  echo "info             runs NSD -info"
  echo "stacks           runs NSD -stacks"
  echo "nsd              runs complete NSD"
  echo "nsdnomem         runs NSD -nomemcheck"
  echo "memdump          generate memory dump"
  echo "hang             use if hanging servers. run 3 NSDs -stacks + one full NSD"
  echo "kill             runs NSD - kill"
  echo "cleanup          cleanup all server resources"
  
  echo
  echo "Log Commands"
  echo
  echo "archivelog       archive logs even if server is running"
  echo "clearlog         clear console logs etc"
  echo "lastlog          show last <n> log lines (default 100 lines)"
  echo "log              shows/edits the log file"
  echo "systemlog        shows last <n> system log lines (default 100 lines)"

  echo
  echo "Config Commands"
  echo
  echo "inivar           show notes.ini variable"
  echo "ini              edit notes.ini"
  echo "listini          lists notes.ini - can be redirected and greped"
  echo "config/cfg       edit start script config file (default | server)"
  echo "systemdcfg       edit systemd config file"
  echo


  echo
  echo "One Touch Setup Commands"
  echo
  echo "setup            edits an existing One Touch config file or creates a 1st server ENV file"
  echo "setup env 1      creates and edits a first server ENV file"
  echo "setup env 2      creates and edits an additional server ENV file"
  echo "setup json 1     creates and edits a first server JSON file"
  echo "setup json 2     creates and edits an additional server JSON file"
  echo
  echo "setup log        lists the One Touch setup log file"
  echo "setup log edit   edits the One Touch setup log file"
  echo

  if [ -e "$DOMINO_DATA_PATH/trace" ]; then
    echo
    echo "Sametime Commands"
    echo
    echo "resetstlogs      reset Sametime log/diag files"
    echo
  fi

  echo

  if [ ! -z "$DOMINO_CUSTOM_COMMAND_BASEPATH" ]; then
    if [ -x "$DOMINO_CUSTOM_COMMAND_BASEPATH" ]; then
      if [ -e "$DOMINO_CUSTOM_COMMAND_BASEPATH/help.txt" ]; then
        echo
        cat "$DOMINO_CUSTOM_COMMAND_BASEPATH/help.txt"
        echo
      fi
    fi
  fi

  return 0
}

cleanup_domino_server_os_level ()
{
  pre_cleanup_operations
  
  # Kill all domino processes including java server controller
  for pid in `$PS_COMMAND -fu $DOMINO_USER | grep "$LOTUS_BIN_DIR" | grep -v "$SCRIPT_NAME" | grep -v "grep" | awk '{ print $2 }'`; do
    echo "Killing pid #" $pid
    kill -9 $pid
  done

  # Kill 3rd party Domino processes started from other directories
  
  
  if [ -z "$DOMINO_3RD_PARTY_BIN_DIRS" ]; then
    DebugText "No 3rd Party Bin Dirs specified"
  else

    for j in $DOMINO_3RD_PARTY_BIN_DIRS; do
      if [ -n "$j" ]; then
       DebugText "Killing 3rd Party Processes for" $j
    
       for pid in `$PS_COMMAND -fu $DOMINO_USER | grep "$j" | grep -v "$SCRIPT_NAME" | grep -v "grep" | awk '{ print $2 }'`; do
         echo "Killing 3rd-party process - pid #" $pid
         kill -9 $pid
       done
      fi 
    done
  fi

  # Remove Notes lock file

  if [ -e $DOMINO_DATA/~notes.lck ]; then
    echo "Removing ~notes.lck"
    rm -f $DOMINO_DATA/~notes.lck
  fi

  # Remove Server Controller lock file
  if [ -e $DOMINO_DATA/.jsc_lock ]; then
    echo "Removing .jsc_lock"
    rm -f $DOMINO_DATA/.jsc_lock
  fi
  
  # Remove PID file if present
  if [ -e $DOMINO_PID_FILE ]; then
    echo "Removing $DOMINO_PID_FILE"
    rm $DOMINO_PID_FILE
  fi
  
  # Remove shared mem, semaphores and message-queues 
  if [ ${LARCH} = "ibmpow" ]; then
    mems=`ipcs -m | grep " $DOMINO_USER " | grep ^m | awk '{ print $2 }'`
    sems=`ipcs -s | grep " $DOMINO_USER " | grep ^s | awk '{ print $2 }'`
    mqs=`ipcs  -q | grep " $DOMINO_USER " | grep ^q | awk '{ print $2 }'`
  fi

  if [ ${LARCH} = "sunspa" ]; then
    mems=`ipcs -m | grep " $DOMINO_USER " | awk '{ print $2 }'`
    sems=`ipcs -s | grep " $DOMINO_USER " | awk '{ print $2 }'`
    mqs=`ipcs  -q | grep " $DOMINO_USER " | awk '{ print $2 }'`  
  fi

  if [ ${LARCH} = "linux" ]; then
    mems=`ipcs -m | grep " $DOMINO_USER " | grep ^0x | awk '{ print $2 }'`
    sems=`ipcs -s | grep " $DOMINO_USER " | grep ^0x | awk '{ print $2 }'`
    mqs=`ipcs  -q | grep " $DOMINO_USER " | grep ^0x | awk '{ print $2 }'`
  fi

  for j in $mems; do
    if [ -n "$j" ]; then
      echo "Removing shared memory handle #" $j
      ipcrm -m $j
    fi 
  done

  for j in $sems; do
    if [ -n "$j" ]; then
      echo "Removing semaphore #" $j
      ipcrm -s $j
    fi 
  done

  for j in $mqs; do
    if [ -n "$j" ]; then
      echo "Removing message queue #" $j
      ipcrm -q $j
    fi 
  done

  # Remove memory mapped files on Solaris
  if [ ${LARCH} = "sunspa" ]; then
    mem_files = `find /tmp -name ".NOTESMEM_please_do_not_remove.*" -user "$DOMINO_USER"`

    for mem_file_name in `find /tmp -name ".NOTESMEM_please_do_not_remove.*" -user "$DOMINO_USER"`; do
      echo "Removing memory mapped file #" $mem_file_name
      rm $mem_file_name
    done
  fi

  post_cleanup_operations

  return 0
}

show_domino_resources_short ()
{
  echo "--- Process List ---"

  for pid in `$PS_COMMAND -fu $DOMINO_USER | grep "$LOTUS_BIN_DIR" | grep -v "$SCRIPT_NAME" | grep -v "grep" | awk '{ print $2 }'`; do
    echo "pid #" $pid
  done
  echo

  echo "--- 3rd Party Process List ---"

  if [ -z "$DOMINO_3RD_PARTY_BIN_DIRS" ]; then
    DebugText "No 3rd Party Bin Dirs specified"
  else

    for j in $DOMINO_3RD_PARTY_BIN_DIRS; do
      if [ -n "$j" ]; then
    
       for pid in `$PS_COMMAND -fu $DOMINO_USER | grep "$j" | grep -v "$SCRIPT_NAME" | grep -v "grep" | awk '{ print $2 }'`; do
         echo "3rd-party - pid #" $pid
       done
      fi 
    done
  fi

  echo

  if [ ${LARCH} = "ibmpow" ]; then
    mems=`ipcs -m | grep " $DOMINO_USER " | grep ^m | awk '{ print $2 }'`
    sems=`ipcs -s | grep " $DOMINO_USER " | grep ^s | awk '{ print $2 }'`
    mqs=`ipcs  -q | grep " $DOMINO_USER " | grep ^q | awk '{ print $2 }'`
  fi

  if [ ${LARCH} = "sunspa" ]; then
    mems=`ipcs -m | grep " $DOMINO_USER " | awk '{ print $2 }'`
    sems=`ipcs -s | grep " $DOMINO_USER " | awk '{ print $2 }'`
    mqs=`ipcs  -q | grep " $DOMINO_USER " | awk '{ print $2 }'`  
  fi

  if [ ${LARCH} = "linux" ]; then
    mems=`ipcs -m | grep " $DOMINO_USER " | grep ^0x | awk '{ print $2 }'`
    sems=`ipcs -s | grep " $DOMINO_USER " | grep ^0x | awk '{ print $2 }'`
    mqs=`ipcs  -q | grep " $DOMINO_USER " | grep ^0x | awk '{ print $2 }'`
  fi

  echo "--- Shared Memory ---"
  for j in $mems; do
    if [ -n "$j" ]; then
      echo "#" $j
    fi 
  done
  echo
 
  echo "--- Semaphores ---"
  for j in $sems; do
    if [ -n "$j" ]; then
      echo "#" $j
    fi 
  done
  echo
 
  echo "--- MQs ---"
  for j in $mqs; do
    if [ -n "$j" ]; then
      echo "mq #" $j
    fi 
  done
  echo

  if [ ${LARCH} = "sunspa" ]; then
    echo "--- Solaris Memory Mapped Files  ---"
    mem_files = `find /tmp -name ".NOTESMEM_please_do_not_remove.*" -user "$DOMINO_USER"`

    for mem_file_name in `find /tmp -name ".NOTESMEM_please_do_not_remove.*" -user "$DOMINO_USER"`; do
      echo "#" $mem_file_name
    done
  fi

  echo

  if [ -e $DOMINO_DATA/~notes.lck ]; then
    echo "Lock file exists (~notes.lck)"
  fi

  # Remove Server Controller lock file
  if [ -e $DOMINO_DATA/.jsc_lock ]; then
    echo "Server controller log file exist (.jsc_lock)"
  fi

  echo

  return 0
}


show_domino_resources ()
{
  echo "--- Process List ---"

  $PS_COMMAND -fu $DOMINO_USER | grep "$LOTUS_BIN_DIR" | grep -v "$SCRIPT_NAME" | grep -v "grep"
  echo

  echo "--- 3rd Party Process List ---"

  if [ -z "$DOMINO_3RD_PARTY_BIN_DIRS" ]; then
    DebugText "No 3rd Party Bin Dirs specified"
  else

    for j in $DOMINO_3RD_PARTY_BIN_DIRS; do
      $PS_COMMAND -fu $DOMINO_USER | grep "$j" | grep -v "$SCRIPT_NAME" | grep -v "grep"
    done
  fi

  echo

  if [ ${LARCH} = "ibmpow" ]; then
    echo "--- Shared Memory ---"
    ipcs -m | grep " $DOMINO_USER " | grep ^m
    echo
    echo "--- Semaphores ---"
    ipcs -s | grep " $DOMINO_USER " | grep ^s
    echo
    echo "--- MQs ---"
    ipcs  -q | grep " $DOMINO_USER " | grep ^q
  fi

  if [ ${LARCH} = " sunspa " ]; then
    echo "--- Shared Memory ---"
    ipcs -m | grep " $DOMINO_USER "
    echo
    echo "--- Semaphores ---"
    ipcs -s | grep " $DOMINO_USER "
    echo
    echo "--- MQs ---"
    ipcs  -q | grep " $DOMINO_USER "  
  fi

  if [ ${LARCH} = "linux" ]; then
    echo "--- Shared Memory ---"
    ipcs -m | grep " $DOMINO_USER " | grep ^0x
    echo
    echo "--- Semaphores ---"
    ipcs -s | grep " $DOMINO_USER " | grep ^0x
    echo
    echo "--- MQs ---"
    ipcs  -q | grep " $DOMINO_USER " | grep ^0x
  fi

  echo

  if [ ${LARCH} = "sunspa" ]; then
    echo "--- Solaris Memory Mapped Files  ---"
    find /tmp -name ".NOTESMEM_please_do_not_remove.*" -user "$DOMINO_USER"
  fi

  echo

  if [ -e $DOMINO_DATA/~notes.lck ]; then
    echo "Lock file exists (~notes.lck)"
  fi

  # Remove Server Controller lock file
  if [ -e $DOMINO_DATA/.jsc_lock ]; then
    echo "Server controller log file exist (.jsc_lock)"
  fi

  if [ -e $DOMINO_PID_FILE ]; then
    echo "Domino Server Process-ID file exists ($DOMINO_PID_FILE)"
  fi
 
  return 0
}

show_lastlog ()
{
  if [ -z "$1" ]; then
    LAST_LOG_LINES=100
  else
    LAST_LOG_LINES="$1"
  fi

  echo "--- Last Console Output ($LAST_LOG_LINES) ---"
  $TAIL_CMD -n $LAST_LOG_LINES $REAL_OUTPUT_FILE

  return 0
}

log_status()
{
  echo
  echo "-- Log Status --"
  echo
  echo "Comact Task        : $DOMINO_COMPACT_TASK"
        echo "Compact Options    : $DOMINO_COMPACT_OPTIONS"
        echo "Log Compact Options: $DOMINO_LOG_COMPACT_OPTIONS"
  echo

  
  return 0
}

cleanup_domino_server ()
{
  cd $DOMINO_DATA_PATH
  
  if [ "$DOMINO_NSD_BEFORE_KILL" = "yes" ]; then
    echo " ... running NSD before killing the server"
    nsd_check_set_posix_locale
    $NSD_BIN
  fi
  
  nsd_check_set_posix_locale
  pre_kill_operations
  $NSD_BIN -kill
  post_kill_operations
  return 0
}

cleanup_java_controller ()
{
  # Kill java controller process
  for pid in `$PS_COMMAND -fu $DOMINO_USER | grep "$LOTUS_BIN_DIR" | grep -v "$SCRIPT_NAME" | grep -v "grep" | grep " -jc" | awk '{ print $2 }'`; do
    kill -9 $pid
  done

  if [ -e $DOMINO_DATA_PATH/.jsc_lock ]; then
    rm $DOMINO_DATA_PATH/.jsc_lock
  fi

  return 0
}

compact_dbs ()
{
	cd $DOMINO_DATA_PATH

  if [ ! -z "$DOMINO_COMPACT_OPTIONS" ]; then
  	echo "Running $DOMINO_COMPACT_TASK $DOMINO_COMPACT_OPTIONS"
    echo "---- Compact Output for $DOMINO_COMPACT_TASK $DOMINO_COMPACT_OPTIONS ----" >> $DOMINO_OUTPUT_LOG
    $DOMINO_COMPACT_BIN $DOMINO_COMPACT_OPTIONS >> $DOMINO_OUTPUT_LOG 2>&1
    echo "---- Compact Finished ----" >> $DOMINO_OUTPUT_LOG
  else
  	echo "No Compact Options configured"
  fi

  return 0
}

compact_log ()
{
	cd $DOMINO_DATA_PATH

  if [ ! -z "$DOMINO_LOG_COMPACT_OPTIONS" ]; then
  	echo "Running $DOMINO_COMPACT_TASK $DOMINO_LOG_COMPACT_OPTIONS"
    echo "---- Compact Output for $DOMINO_COMPACT_TASK $DOMINO_LOG_COMPACT_OPTIONS ----" >> $DOMINO_OUTPUT_LOG
    $DOMINO_COMPACT_BIN $DOMINO_LOG_COMPACT_OPTIONS >> $DOMINO_OUTPUT_LOG 2>&1
    echo "---- Compact Finished ----" >> $DOMINO_OUTPUT_LOG
  else
  	echo "No Log Compact Options configured"
  fi

  return 0
}

fixup_dbs ()
{
	cd $DOMINO_DATA_PATH

  if [ ! -z "$DOMINO_FIXUP_OPTIONS" ]; then
  	echo "Running fixup $DOMINO_FIXUP_OPTIONS"

    echo "---- Fixup Output for Fixup $DOMINO_FIXUP_OPTIONS ----" >> $DOMINO_OUTPUT_LOG

    $DOMINO_FIXUP_BIN $DOMINO_FIXUP_OPTIONS >> $DOMINO_OUTPUT_LOG 2>&1

    echo "---- Fixup terminated ----" >> $DOMINO_OUTPUT_LOG
  else
  	echo "No Fixup Options configured"
  fi
}

pre_shutdown_command ()
{
  if [ -z "$DOMINO_PRE_SHUTDOWN_COMMAND" ]; then
    return 0
  fi
  
  cd $DOMINO_DATA_PATH
  $DOMINO_BIN -c "$DOMINO_PRE_SHUTDOWN_COMMAND" >> $DOMINO_OUTPUT_LOG 2>&1 &
  echo "Running pre shutdown command '$DOMINO_PRE_SHUTDOWN_COMMAND'"

  if [ -z "$DOMINO_PRE_SHUTDOWN_DELAY" ]; then
    return 0
  fi

  echo "Waiting $DOMINO_PRE_SHUTDOWN_DELAY seconds after pre-shutdown command"
  sleep $DOMINO_PRE_SHUTDOWN_DELAY

  return 0
}

shutdown_domino_server ()
{
  domino_is_running

  if [ $? -eq 0 ]; then
    echo "Domino Server is not running - No Shutdown needed"
    return 0
  fi

  pre_shutdown_command 
  pre_shutdown_operations
  
  cd $DOMINO_DATA_PATH
  $DOMINO_BIN -q >> $DOMINO_OUTPUT_LOG 2>&1 &

  echo " ... waiting for shutdown to complete"

  seconds=0
  domino_is_running
  while [ $? -eq 1 ]; do
    sleep 1
    seconds=$(expr $seconds + 1)
    sec_mod=$(expr $seconds "%" 10)
    if [ "$sec_mod" = "0" ]; then
      echo " ... waiting $seconds seconds"
    fi

    # Check Shutdown Delayed commands

    check_shutdown_delayed_commands $seconds

    # Terminate Domino Server if still running
    if [ "$seconds" -ge "$DOMINO_SHUTDOWN_TIMEOUT" ]; then
      echo "Domino Server is still running after $seconds seconds"
      echo " ... now for the ungraceful method"
      cleanup_domino_server
      echo "Domino Server ($DOMINO_SERVER) TERMINATED"
   
      if [ "$DOMINO_DEBUG_MODE" = "yes" ]; then
        domino_is_running
        if [ $? -eq 1 ]; then
          echo "Domino Server Binaries are still running after termination via NSD!"
        fi
      fi
   
      return 1
    fi
    domino_is_running
  done

  # Remove PID file if present
  if [ -e $DOMINO_PID_FILE ]; then
    rm $DOMINO_PID_FILE
  fi


  if [ "$DOMINO_ARCHIVE_LOGS_SHUTDOWN" = "yes" ]; then
    archive_logs
  fi

  post_shutdown_operations

 echo "Domino stopped ($seconds sec)"

  return 0
}

shutdown_java_controller ()
{
  java_controller_is_running

  if [ $? -eq 0 ]; then
    echo "Server Controller not running - No Shutdown needed"
    return 0
  fi
  
  echo y | $DOMINO_BIN -jc -q -y >> $DOMINO_OUTPUT_LOG 2>&1 &
  echo $DOMINO_BIN -jc -q
  count=0
  java_controller_is_running
  
  while [ $? -eq 1 ]; do
    sleep 10
    count=`expr $count + 1`
    if [ $count -ge 10 ]; then
      # kill java controller and lock file
      cleanup_java_controller
    fi      
    java_controller_is_running
  done

  return 0
}

shutdown_broadcast_message ()
{
  if [ "$DOMINO_BROADCAST_SHUTDOWN_MESSAGE" = "yes" ]; then
    $DOMINO_BIN -c ' BROADCAST "(!) Domino Server will be stopped now"' >> $DOMINO_OUTPUT_LOG 2>&1
  fi

  return 0
}

check_path_from_ini_writable()
{
  get_notes_ini_var "$DOMINO_INI_PATH" "$1"
  DebugText "Check Notes.ini [$1] = '$ret_ini_var'"
  if [ ! -z "$ret_ini_var" ]; then
  
    case "$ret_ini_var" in
    /*) 
      CHECK_PATH="$ret_ini_var"
      ;;
    *)
      CHECK_PATH="$DOMINO_DATA_PATH/$ret_ini_var"
      ;;
    esac

    DebugText "Check Notes.ini [$1] = '$CHECK_PATH'"
  
    if [ ! -w $CHECK_PATH ]; then
      echo "Warning: Cannot write to [$1] '$ret_ini_var' ($CHECK_PATH)"
    fi
  fi
  return 0
}

check_res_file_warnings()
{
  ret_res=0

  if [ -z "$LANG" ]; then
    echo "Warning: LANG is not set!"
  else
    RES_FILE=$Notes_ExecDirectory/res/$LANG/strings.res
    #echo "RES_FILE: '$RES_FILE'"

    if [ -r "$RES_FILE" ]; then
      ret_res=1
    fi
  fi

  RES_FILE=$Notes_ExecDirectory/res/C/strings.res
  #echo "RES_FILE: '$RES_FILE'"

  if [ -r "$RES_FILE" ]; then
    ret_res=1
  fi

  if [ $ret_res -eq 0 ]; then
    echo "Warning: Cannot read 'res' files. Check '$Notes_ExecDirectory/res' for correct LANG setting"
  fi

  return 0
}


checkset_startcompact_options()
{
  case "$1" in 
    on)
      echo
      if [ -e $DOMINO_NEXTSTART_COMPACT_STATUSFILE ]; then
        echo "Compact next start already -> ON"
      else
        echo "Compact next start -> ON"
        touch $DOMINO_NEXTSTART_COMPACT_STATUSFILE
      fi
      echo
      ;;
    
    off)

      echo
      if [ -e $DOMINO_NEXTSTART_COMPACT_STATUSFILE ]; then
        echo "Compact next start -> OFF"
        rm $DOMINO_NEXTSTART_COMPACT_STATUSFILE
      else
        echo "Compact next start already -> OFF"
      fi
      echo
      ;;

    *)
      echo
      if [ -e $DOMINO_NEXTSTART_COMPACT_STATUSFILE ]; then

        echo "Compact next start : ON"
        echo
        echo "Comact Task        : $DOMINO_COMPACT_TASK"
        echo "Compact Options    : $DOMINO_COMPACT_OPTIONS"
        echo "Log Compact Options: $DOMINO_LOG_COMPACT_OPTIONS"
        
      else
        echo "Compact next start : OFF"
      fi
      echo
      ;;
  esac

  return 0
}


check_startup_warnings()
{
  check_path_from_ini_writable "directory"
  check_path_from_ini_writable "view_rebuild_dir"
  check_path_from_ini_writable "notes_tempdir"
  check_path_from_ini_writable "FTBasePath"
  
  # check translog writable
  get_notes_ini_var "$DOMINO_INI_PATH" "translog_status"
  if [ ! -z "$ret_ini_var" ]; then
    if [ ! "$ret_ini_var" = "0" ]; then
      check_path_from_ini_writable "translog_path"
    fi
  fi

  # check DAOS writable
  get_notes_ini_var "$DOMINO_INI_PATH" "DAOSENABLE"
  if [ ! -z "$ret_ini_var" ]; then
    if [ ! "$ret_ini_var" = "0" ]; then
      check_path_from_ini_writable "DAOSBASEPATH"
    fi
  fi
 
  get_notes_ini_var "$DOMINO_INI_PATH" "LAST_DOMINO_TIME_ABORT"
  if [ ! -z "$ret_ini_var" ]; then
    echo "Warning: Notes.ini LAST_DOMINO_TIME_ABORT set to '$ret_ini_var'"
  fi

  if [ ! -x $LOTUS/bin/dbmt ]; then
    echo "Warning: Cannot access '$LOTUS/bin/dbmt' -- Create a link e.g. 'ln -s tools/startup dbmt'"
  fi

  check_res_file_warnings

  return 0
}

check_server_start_abort_error()
{
  # the following notes.ini settings are involoved in this functionality
  # if the time interval is elapsed at restart your server will not start and you see
  # no error message on console, just a new notes.ini entry LAST_DOMINO_TIME_ABORT
  
  # Restart_Time_Action=1
  # Restart_Time_Interval=3600 (in seconds, smallest value is 3600)
  # LAST_DOMINO_TIME_ABORT=16.07.2014 09:56:49 Startup aborted: Excess restart time interval. 
  
  get_notes_ini_var "$DOMINO_INI_PATH" "Restart_Time_Action"
  if [ -z "$ret_ini_var" ]; then
    DebugText "Notes.ini Restart_Time_Action not set"
    return 0
  fi
  if [ "$ret_ini_var" = "0" ]; then
    DebugText "Notes.ini Restart_Time_Action disabled"
    return 0
  fi

  echo "Waiting if server started .. (5 seconds)" 
  sleep 5
  echo

  domino_is_running
  if [ $? -eq 1 ]; then
    return 0
  fi

  echo
  echo "Warning: Domino is not running after startup!!!"
  echo

  get_notes_ini_var "$DOMINO_INI_PATH" "LAST_DOMINO_TIME_ABORT"
  if [ ! -z "$ret_ini_var" ]; then
    echo "Warning: Server start aborted: $ret_ini_var"
    echo "--> Check Machine Time and reset LAST_DOMINO_TIME / LAST_DOMINO_TIME_ABORT"
    echo
    return 1
  else
    echo "Please check Server manually"
    echo  
  fi

  echo

  return 0
}

reset_sametime_logs()
{
  local ST_LOG_DIR=$DOMINO_DATA_PATH/trace

  if [ -e "$ST_LOG_DIR" ]; then

    find $ST_LOG_DIR -name "*.diag" -exec $REMOVE_COMMAND_TEMP '{}' \;
    find $ST_LOG_DIR -name "*.lck" -exec $REMOVE_COMMAND_TEMP '{}' \;
    find $ST_LOG_DIR -name "*.txt" -exec $REMOVE_COMMAND_TEMP '{}' \;
    find $ST_LOG_DIR -name "*.log" -exec $REMOVE_COMMAND_TEMP '{}' \;

    echo > $DOMINO_DATA_PATH/sametime.log

    echo
    echo "Sametime Diagnostic logs reset"
    echo

  else

    echo
    echo "No Sametime log directory found"
    echo
  fi

  return 0
}

check_one_touch_setup()
{
  echo "Checking OneTouchSetup" >> $DOMINO_OUTPUT_LOG
  # Run OneTouchSetup if server is not configured yet and config files exist

  if [ -n "$(grep -i "ServerSetup=" $DOMINO_DATA_PATH/notes.ini)" ]; then
    # Disabled OnTouchSetup on already configured server
    export SetupAutoConfigure=
    echo Domino already configured >> $DOMINO_OUTPUT_LOG
    return 0
  fi

  # Check OnTouchSetup files only if not already specified (for example for container setup)
  if [ -n "$SetupAutoConfigure" ]; then
    echo "One Touch already set - Skipping auto detect config"  >> $DOMINO_OUTPUT_LOG
    return 1
  fi

  DOMINO_AUTO_CONFIG_ENV_FILE=$DOMINO_DATA_PATH/DominoAutoConfig.env
  DOMINO_AUTO_CONFIG_JSON_FILE=$DOMINO_DATA_PATH/DominoAutoConfig.json

  if [ -e "$DOMINO_AUTO_CONFIG_JSON_FILE" ]; then
    export SetupAutoConfigureParams=$DOMINO_AUTO_CONFIG_JSON_FILE
    export SetupAutoConfigure=1
    echo Domino One Touch setup via $DOMINO_AUTO_CONFIG_JSON_FILE >> $DOMINO_OUTPUT_LOG
    return 1
  fi

  if [ -e "$DOMINO_AUTO_CONFIG_ENV_FILE" ]; then

    # Source in environment vars
    set -a
    . $DOMINO_AUTO_CONFIG_ENV_FILE
    set +a
    export SetupAutoConfigure=1
    echo Domino One Touch setup via $DOMINO_AUTO_CONFIG_ENV_FILE >> $DOMINO_OUTPUT_LOG
    export SERVERSETUP_SERVER_TYPE
    return 1

  fi

  echo "No OneTouchConfig found" >> $DOMINO_OUTPUT_LOG
}

cleanup_one_touch_setup()
{

  # In any case remove OneTouchSetup files after setup

  remove_file "$DOMINO_AUTO_CONFIG_ENV_FILE"

  # JSON file is deleted by setup when done.

}

DebugText "==== Start Script Debug Begin ===="

# --- Sanity check for server environment ---

# Check if script is executed with root user

if [ "$DOMINO_USER" = "root" ]; then
  errmsg_and_exit "Run the Domino Server as a notes user - exiting "
fi

# Check rights to execute domino binaries
if [ ! -x $DOMINO_BIN ]; then
  errmsg_and_exit "Cannot access server command ($DOMINO_BIN) - exiting"
fi

# Check notes-data directory
if [ ! -w $DOMINO_DATA_PATH ]; then
  errmsg_and_exit "Cannot access notes data directory ($DOMINO_DATA_PATH) - exiting"
fi

# Check right to write to notes.ini
if [ ! -w $DOMINO_INI_PATH ]; then
  errmsg_and_exit "Cannot write to Notes.ini ($DOMINO_INI_PATH) - exiting"
fi

# Needed in different places
DOMINO_IBM_TECHNICAL_SUPPORT_DIR=$DOMINO_DATA_PATH/IBM_TECHNICAL_SUPPORT

# Ensure we have the right log files for server controller and Docker direct output mode
CONTROLLER_FILE=""
if [ "$DOMINO_USE_JAVA_CONTROLLER" = "yes" ]; then

  get_notes_ini_var "$DOMINO_INI_PATH" "DominoControllerCurrentLog"
  DebugText "DominoControllerCurrentLog = '$ret_ini_var'"
  echo  "DominoControllerCurrentLog = $DominoControllerCurrentLog"
  CONTROLLER_FILE=$ret_ini_var

  # newer versions use absolute path
  case "$CONTROLLER_FILE" in
    /*) REAL_OUTPUT_FILE=/$CONTROLLER_FILE ;;
     *) REAL_OUTPUT_FILE=$DOMINO_DATA_PATH/$CONTROLLER_FILE ;;
  esac

else
  REAL_OUTPUT_FILE=$DOMINO_OUTPUT_LOG
fi

# If normal log is not there, use console.log
if [ ! -e "$DOMINO_OUTPUT_LOG" ]; then
  REAL_OUTPUT_FILE=$DOMINO_IBM_TECHNICAL_SUPPORT_DIR/console.log
  echo  "Using $REAL_OUTPUT_FILE"
fi

# --- Main script logic ---

# Special platform tuning parameters

if [ ${LARCH} = "linux" ]; then

  # Linux platform tuning parameters
      
  if [ -n "$DOMINO_SHARED_DPOOLSIZE" ]; then
    Notes_SHARED_DPOOLSIZE=$DOMINO_SHARED_DPOOLSIZE
    export Notes_SHARED_DPOOLSIZE
  fi
fi

# Environment settings needed for Domino servers

if [ ! -z "$DOMINO_LANG" ]; then
  LANG=$DOMINO_LANG
  export LANG
fi

if [ ! -z "$DOMINO_UMASK" ]; then
  umask $DOMINO_UMASK
fi

# Ensure that LC_ALL is reset because on SuSE /etc/rc.status sets it to POSIX
# LC_ALL overwrites LANG and other LC settings
# LANG is used by the Domino server and LC_ALL would overwrite it
# On the other side you need POSIX for some operations in NSD and should not set LC_ALL to your locale

LC_ALL=
export LC_ALL

dump_configuration
DebugText "ALL PARAMETERS:" $@

case "$PARAM1" in
	  
  start)

    echo

    if [ "$PARAM2" = "live" ]; then
      DOMINO_LIVE_START="yes"
    fi

    domino_is_running
    if [ $? -eq 1 ]; then
      errmsg_and_exit "Domino Server is already running - exiting"
    fi

    if [ -e "$DOMINO_DATA_PATH/domino_disabled" ]; then
      errmsg_and_exit "Domino Server is disabled for Maintenance - Server NOT started"
    fi

    cd $DOMINO_DATA_PATH
    # cleanup

    # Terminate java controlle before restarting domino server with java controller
    if [ "$DOMINO_USE_JAVA_CONTROLLER" = "yes" ]; then
      java_controller_is_running
      if [ $? -eq 1 ]; then
        shutdown_java_controller
      fi
    fi 

    DOMINO_ARCHIVE_LOG_BACKGROUND="yes"
    archive_logs    
    startup_remove_file_check
    check_create_directories

    check_one_touch_setup

    # Empty the input file
    cat /dev/null > $DOMINO_INPUT_FILE

    echo
    echo "Starting Domino for $PLATFORM_NAME ($DOMINO_SERVER)"

    DOMINO_START_DATETIME=`date`
    echo "---- Starting at $DOMINO_START_DATETIME  ----" >> $DOMINO_OUTPUT_LOG
    echo >> $DOMINO_OUTPUT_LOG
    echo "---- environment info ----" >> $DOMINO_OUTPUT_LOG
    echo >> $DOMINO_OUTPUT_LOG
    set >> $DOMINO_OUTPUT_LOG
    echo >> $DOMINO_OUTPUT_LOG

    echo "---- locale info ----" >> $DOMINO_OUTPUT_LOG
    echo >> $DOMINO_OUTPUT_LOG
    locale >> $DOMINO_OUTPUT_LOG
    echo >> $DOMINO_OUTPUT_LOG

    echo >> $DOMINO_OUTPUT_LOG
    echo "---- umask ----" >> $DOMINO_OUTPUT_LOG
    echo >> $DOMINO_OUTPUT_LOG
    umask >> $DOMINO_OUTPUT_LOG
    
    if [ ${LARCH} = "ibmpow" ]; then

      # AIX platform tuning parameters

      echo >> $DOMINO_OUTPUT_LOG
      echo "---- ulimit soft info ----" >> $DOMINO_OUTPUT_LOG
      echo >> $DOMINO_OUTPUT_LOG
      ulimit -a >> $DOMINO_OUTPUT_LOG

      echo >> $DOMINO_OUTPUT_LOG
      echo "---- ulimit hard info ----" >> $DOMINO_OUTPUT_LOG
      echo >> $DOMINO_OUTPUT_LOG
      ulimit -a -H >> $DOMINO_OUTPUT_LOG

      echo >> $DOMINO_OUTPUT_LOG
      echo "---- no info ----" >> $DOMINO_OUTPUT_LOG
      echo >> $DOMINO_OUTPUT_LOG
      no -a >> $DOMINO_OUTPUT_LOG
    fi

    if [ ${LARCH} = "sunspa" ]; then

      # Solaris platform tuning parameters

      echo >> $DOMINO_OUTPUT_LOG
      echo "---- ulimit soft info ----" >> $DOMINO_OUTPUT_LOG
      echo >> $DOMINO_OUTPUT_LOG
      ulimit -a >> $DOMINO_OUTPUT_LOG

      echo >> $DOMINO_OUTPUT_LOG
      echo "---- ulimit hard info ----" >> $DOMINO_OUTPUT_LOG
      echo >> $DOMINO_OUTPUT_LOG
      ulimit -a -H >> $DOMINO_OUTPUT_LOG
    fi

    if [ ${LARCH} = "linux" ]; then

      echo >> $DOMINO_OUTPUT_LOG
      echo "---- ulimit soft info ----" >> $DOMINO_OUTPUT_LOG
      echo >> $DOMINO_OUTPUT_LOG
      ulimit -a >> $DOMINO_OUTPUT_LOG

      echo >> $DOMINO_OUTPUT_LOG
      echo "---- ulimit hard info ----" >> $DOMINO_OUTPUT_LOG
      echo >> $DOMINO_OUTPUT_LOG
      ulimit -a -H >> $DOMINO_OUTPUT_LOG
    fi
    
    echo >> $DOMINO_OUTPUT_LOG
    echo "---- end of info messages ----" >> $DOMINO_OUTPUT_LOG
    echo >> $DOMINO_OUTPUT_LOG

    pre_startup_operations
    
    if [ ! "$DOMINO_CHECK_WARNUNGS" = "no" ]; then
      check_startup_warnings
    fi 

    # check if domlog.nsf should be renamed before compacts
    check_domlog_db_rename

    if [ ! -z "$DOMINO_START_FIXUP_OPTIONS" ]; then
      DOMINO_FIXUP_OPTIONS=$DOMINO_START_FIXUP_OPTIONS
      fixup_dbs
    fi

    if [ -e "$DOMINO_NEXTSTART_COMPACT_STATUSFILE" ]; then
      rm "$DOMINO_NEXTSTART_COMPACT_STATUSFILE"
      
      compact_dbs
      
      # check if log.nsf should be renamed after normal compacts & before log.nsf compact
      check_log_db_rename
      compact_log
    else
      if [ ! -z "$DOMINO_START_COMPACT_OPTIONS" ]; then
        DOMINO_COMPACT_OPTIONS=$DOMINO_START_COMPACT_OPTIONS
        compact_dbs
      fi

      # check if log.nsf should be renamed after normal compacts & before log.nsf compact
      check_log_db_rename

      if [ ! -z "$DOMINO_START_LOG_COMPACT_OPTIONS" ]; then
        DOMINO_LOG_COMPACT_OPTIONS=$DOMINO_START_LOG_COMPACT_OPTIONS
        compact_log
      fi
    fi
    
    if [ "$DOMINO_DOCKER_STDOUT" = "yes" ]; then

      echo "---- Docker Container Output ----"
      cat $DOMINO_OUTPUT_LOG
      rm "$DOMINO_OUTPUT_LOG"
      $DOMINO_BIN $JAVA_CONTROLLER_STRING &

    else
      $DOMINO_BIN $JAVA_CONTROLLER_STRING < $DOMINO_INPUT_FILE >> $DOMINO_OUTPUT_LOG 2>&1 &
    fi

    DOMINO_PID=$!
 
    # For servers using id-file passwords,
    # uncomment the following line.
    # cat .secure_hidden_passwd > $DOMINO_INPUT_FILE
    
    echo $DOMINO_PID > $DOMINO_PID_FILE
    echo "done PID is $DOMINO_PID"
    echo
    
    # Remember status and be verbose
    rc_status -v

    cleanup_one_touch_setup

    post_startup_operations

    if [ "$DOMINO_LIVE_START" = "yes" ]; then
      run_monitor      
    fi

    if [ ! "$DOMINO_CHECK_WARNUNGS" = "no" ]; then
      check_server_start_abort_error
    fi 

    echo
    ;;

  archivelog)
  
    archive_logs "$PARAM2"
      
    # Remember status and be verbose
    rc_status -v
    echo
    ;;

  clearlog)
  
    echo
    log_clear_operations "$PARAM2" "$PARAM3"
       
    # Remember status and be verbose
    echo
    rc_status -v
    echo
    ;;

  stop)

    if [ "$PARAM2" = "live" ]; then
      DOMINO_LIVE_SHUTDOWN="yes"
    fi

    echo
    echo "Stopping Domino for $PLATFORM_NAME ($DOMINO_SERVER)"
    shutdown_broadcast_message

    if [ "$DOMINO_LIVE_SHUTDOWN" = "yes" ]; then
      echo "Starting Live Console for Shutdown ..."
      $TAIL_CMD -f $REAL_OUTPUT_FILE &
      MONITOR_PID=$!

      # signal child died causes issues in bash 5.x
      case "$BASH_VERSION" in
        5*)
          trap "stop_server" 1 2 3 4 6 9 13 15 19 23
          ;;
        *)
          trap "stop_server" 1 2 3 4 6 9 13 15 17 19 23
          ;;
      esac

    fi
    
    shutdown_domino_server

    # Remember status and be verbose
    rc_status -v

    if [ "$DOMINO_USE_JAVA_CONTROLLER" = "yes" ]; then

      echo "Stopping Java Controler for $PLATFORM_NAME ($DOMINO_SERVER)"
      shutdown_java_controller

      echo "Java Controler for $PLATFORM_NAME ($DOMINO_SERVER) shutdown completed"
    fi

    check_for_crashed_server

    echo "Domino for $PLATFORM_NAME ($DOMINO_SERVER) shutdown completed"

    if [ "$DOMINO_LIVE_SHUTDOWN" = "yes" ]; then
      stop_monitor
    fi
    echo
    ;;

  quit)

    if [ "$PARAM2" = "live" ]; then
      DOMINO_LIVE_SHUTDOWN="yes"
    fi

    echo
    echo "Stopping Domino for $PLATFORM_NAME ($DOMINO_SERVER)"
    shutdown_broadcast_message

    if [ "$DOMINO_LIVE_SHUTDOWN" = "yes" ]; then
      echo "Starting Live Console for Shutdown ..."
      $TAIL_CMD -f $REAL_OUTPUT_FILE &
      MONITOR_PID=$!

      # signal child died causes issues in bash 5.x
      case "$BASH_VERSION" in
        5*)
          trap "stop_server" 1 2 3 4 6 9 13 15 19 23
          ;;
        *)
          trap "stop_server" 1 2 3 4 6 9 13 15 17 19 23
          ;;
      esac

    fi
    
    shutdown_domino_server

    # Remember status and be verbose
    rc_status -v

    check_for_crashed_server

    echo "Domino for $PLATFORM_NAME ($DOMINO_SERVER) shutdown completed"

    if [ "$DOMINO_LIVE_SHUTDOWN" = "yes" ]; then
      stop_monitor
    fi
    echo
    ;;

  monitor)
  
    echo
    run_monitor filter
    echo
    ;;

  live)
  
    echo
    run_monitor filter
    echo
    ;;

  console)
  
    echo
    run_monitor
    echo
    ;;

  restart)
  
    # Stop the service and regardless of whether it was running or not, start it again.
    $0 stop "$2" "$3"
    $0 start "$2" "$3"

    echo

    # Remember status and be quiet
    rc_status
    echo
    ;;

  compactnextstart)
    checkset_startcompact_options $PARAM2 $PARAM3
    ;;

  restartcompact)

    if [ ! -z "$PARAM2" ]; then
      DOMINO_COMPACT_OPTIONS=$PARAM2
    fi

    echo

    # Stop the service and Compact databases.
    $0 stop "$2" "$3"

    compact_dbs
    compact_log
    $0 start "$2" "$3"

    # Remember status and be quiet
    rc_status
    echo
    ;;

  compact)

    # Compact only if server is not running.
    if [ ! -z "$PARAM2" ]; then
      DOMINO_COMPACT_OPTIONS=$PARAM2
    fi

    domino_is_running
    if [ $? -eq 1 ]; then
      errmsg_and_exit "Domino Server is running - exiting"
    fi

    echo
    compact_dbs
    compact_log

    # Remember status and be quiet
    rc_status
    echo
    ;;

  restartfixup)

    if [ ! -z "$PARAM2" ]; then
      DOMINO_FIXUP_OPTIONS=$PARAM2
    fi

    # Stop the service and Fixup databases.
    $0 stop "$2" "$3"

    echo

    fixup_dbs
    $0 start "$2" "$3"

    # Remember status and be quiet
    rc_status
    echo
    ;;

  fixup)

    # fixup only if server is not running.

    if [ ! -z "$PARAM2" ]; then
      DOMINO_FIXUP_OPTIONS=$PARAM2
    fi

    domino_is_running
    if [ $? -eq 1 ]; then
      errmsg_and_exit "Domino Server is running - exiting"
    fi

    echo

    fixup_dbs

    # Remember status and be quiet
    rc_status
    echo
    ;;

  cmd)
    echo
    if [ ! -z "$PARAM2" ]; then
      # Send a console command.
      cd $DOMINO_DATA_PATH
      $DOMINO_BIN -c "$PARAM2" >> $DOMINO_OUTPUT_LOG 2>&1 &
      echo $DOMINO_BIN -c "'$PARAM2'"
   
      if [ ! -z "$PARAM3" ]; then
        sleep 5 
        echo 
        echo " --- Console Output for " $PARAM2 " ---"
        $TAIL_CMD -n $PARAM3 $REAL_OUTPUT_FILE
        echo 
        echo "--- End of Console Output ---"
        echo 
      fi 
    else
      echo "Error - no command specified"
    fi
    echo
    ;;

  lastlog)
  
    echo
    show_lastlog "$PARAM2" "$PARAM3" "$PARAM4"
    echo
    exit 0
    ;;

  logstatus)
  
    echo
    log_status "$PARAM2" "$PARAM3" "$PARAM4"
    echo
    exit 0
    ;;

  log)
  
    echo
    show_log "$PARAM2" "$PARAM3" "$PARAM4"
    echo
    exit 0
    ;;

  systemdcfg)
  
    echo
    edit_systemd_cfg "$PARAM2" "$PARAM3" "$PARAM4"
    echo
    exit 0
    ;;


  check)
  
    echo
    check_startup_warnings "$PARAM2"
    echo
    exit 0
    ;;

  memdump)
  
    # Write Memory Dump
    echo
    echo "Creating Memory Dump for ($DOMINO_SERVER)"
    cd $DOMINO_DATA_PATH
    $DOMINO_BIN -m
    echo
    exit 0
    ;;
    
  fullnsd)
  
    # Run NSD 
    echo
    echo "Running full NSD for ($DOMINO_SERVER)"
    cd $DOMINO_DATA_PATH
    nsd_check_set_posix_locale
    $NSD_BIN "$PARAM2" "$PARAM3" "$PARAM4" "$PARAM5" "$PARAM6"
    echo
    exit 0
    ;;

  nsd)
  
    # Run NSD
    echo
    echo "Running NSD for ($DOMINO_SERVER)"
    cd $DOMINO_DATA_PATH
    nsd_check_set_posix_locale
    $NSD_BIN "$PARAM2" "$PARAM3" "$PARAM4" "$PARAM5" "$PARAM6"
    echo
    exit 0
    ;;

  nsdnomem)
  
    # Run NSD -nomemcheck
    echo
    echo "Running NSD -nomemcheck for ($DOMINO_SERVER)"
    cd $DOMINO_DATA_PATH
    nsd_check_set_posix_locale
    
    $NSD_BIN -nomemcheck "$PARAM2" "$PARAM3" "$PARAM4" "$PARAM5" "$PARAM6"
    echo
    exit 0
    ;;

  hang)
  
    # Run 3 x NSD with stacks only + 1 full NSD
    cd $DOMINO_DATA_PATH
    nsd_check_set_posix_locale

    echo "Running #1 NSD -stacks for ($DOMINO_SERVER)"
    $NSD_BIN -stacks "$PARAM2" "$PARAM3" "$PARAM4" "$PARAM5" "$PARAM6"

    echo "Running #2 NSD -stacks for ($DOMINO_SERVER)"
    $NSD_BIN -stacks "$PARAM2" "$PARAM3" "$PARAM4" "$PARAM5" "$PARAM6"

    echo "Running #3 NSD -stacks for ($DOMINO_SERVER)"
    $NSD_BIN -stacks "$PARAM2" "$PARAM3" "$PARAM4" "$PARAM5" "$PARAM6"

    echo "Finally running Full NSD for ($DOMINO_SERVER)"
    $NSD_BIN "$PARAM2" "$PARAM3" "$PARAM4" "$PARAM5" "$PARAM6"

    echo
    echo "NSDs complete"
    echo
    exit 0
    ;;

  stacks)
  
    # Run NSD stacks only
    cd $DOMINO_DATA_PATH
    nsd_check_set_posix_locale

    echo
    echo "Running NSD -stacks for ($DOMINO_SERVER)"
    $NSD_BIN -stacks "$PARAM2" "$PARAM3" "$PARAM4" "$PARAM5" "$PARAM6"

    echo
    echo "NSD complete"
    echo
    exit 0
    ;;
 
  info)
  
    # Run NSD -info
    echo
    echo "Running NSD -info for ($DOMINO_SERVER)"
    cd $DOMINO_DATA_PATH
    nsd_check_set_posix_locale
    $NSD_BIN -info "$PARAM2" "$PARAM3" "$PARAM4" "$PARAM5" "$PARAM6"
    echo
    exit 0
    ;;
    
  kill)
  
    # Run NSD -kill
    echo
    echo "Running NSD -kill for ($DOMINO_SERVER)"
    cd $DOMINO_DATA_PATH
    nsd_check_set_posix_locale
    pre_kill_operations
    $NSD_BIN -kill "$PARAM2" "$PARAM3" "$PARAM4" "$PARAM5" "$PARAM6"
    post_kill_operations
    echo
    exit 0
    ;;

  cleanup)
  
    # Cleanup Server on OS Level
    echo
    echo "Cleaning up Server Resources on OS-Level for ($DOMINO_SERVER)"
    cleanup_domino_server_os_level
    echo
    exit 0
    ;;
  
  resources)
  
    # Show the resources the server is using (processes, shared mem, MQs, SEMs)
    echo
    echo "Show Server Resources ($DOMINO_SERVER)"
    echo
    show_domino_resources
    echo
    exit 0
    ;;

  res)

    # Show the resources the server is using (processes, shared mem, MQs, SEMs)
    echo
    echo "Show Server Resources ($DOMINO_SERVER)"
    echo
    show_domino_resources
    echo
    exit 0
    ;;
     
  status)

    pre_status_operations
    domino_status_check "$PARAM2"

    # Remember status
    domino_status=$?
    _rc_status=$domino_status

    if [ ! "$PARAM" = "display" ]; then
      rc_status -v
    fi

    exit $domino_status
    ;;

  setenv)
  
    # Just set the environment and exit
    # needs to be sourced to keep the environment changes
    # sourced scripts need return instead of exit

    echo
    echo "Environment set ($DOMINO_SERVER)"
    echo
    return 0
    ;;

  version)
  
    # print the script version
    echo
    show_version
    echo
    exit 0
    ;;

  inivar)
  
    # print notes.ini variable
    echo
    if [ ! -z "$PARAM2" ]; then
      get_notes_ini_var "$DOMINO_INI_PATH" "$PARAM2"
      echo "$ret_ini_var"
    fi
    echo
    exit 0
    ;;
  
  ini)
  
    # edit notes.ini
    echo
    $EDIT_COMMAND "$DOMINO_INI_PATH"
    echo
    exit 0
    ;;


  listini)
  
    # dump notes.ini
    echo
    cat "$DOMINO_INI_PATH"
    exit 0
    ;;

  config)
  
    # edit start script config
    echo
    edit_config_file "$PARAM2"
    echo
    exit 0
    ;;

  setup)

    # setup On Touch Setup
    echo
    if [ -e "$SCRIPT_DIR_NAME/DominoOneTouchSetup.sh" ]; then
      . "$SCRIPT_DIR_NAME/DominoOneTouchSetup.sh" "$PARAM2" "$PARAM3" "$PARAM4" "$PARAM5"
    else
      echo "Domino One-Touch Setup not available!"
    fi
    echo
    exit 0
    ;;

  cfg)

    # edit start script config
    echo
    edit_config_file "$PARAM2"
    echo
    exit 0
    ;;

  listcfg)

    # list start script config
    echo
    list_config_file "$PARAM2"
    echo
    exit 0
    ;;

 tika)

    # tika check and stop 
    echo
    tika_server "$PARAM2"
    echo
    exit 0
    ;;

  debuglog)

    # show debug log if present
    echo
    edit_debug_file "$PARAM2"
    echo
    exit 0
    ;;

  resetstlogs)

    reset_sametime_logs
    ;;

  about)

    if [ -x $SCRIPT_DIR_NAME/nshinfo.sh ]; then
      $SCRIPT_DIR_NAME/nshinfo.sh
    fi
    ;;

  About|about+)

    if [ -x $SCRIPT_DIR_NAME/nshinfo.sh ]; then
      $SCRIPT_DIR_NAME/nshinfo.sh ipinfo
    fi
    ;;

  help)
  
    echo
    help
    echo
    exit 0
    ;;

   -\?)
  
    echo
    help
    echo
    exit 0
    ;;

  *)

    if [ -z "$PARAM1" ]; then
      usage 
      exit 1
    fi
  
    if [ -z "$DOMINO_CUSTOM_COMMAND_BASEPATH" ]; then
      DebugText "Invalid PARAM1:" [$PARAM1]
      echo 
      echo "Invalid command:" [$PARAM1]
      usage 
      exit 1
    fi

    DOMINO_CUSTOM_COMMAND_SCRIPT="$DOMINO_CUSTOM_COMMAND_BASEPATH/$PARAM1"
    DebugText "DOMINO_CUSTOM_COMMAND_SCRIPT:" [$DOMINO_CUSTOM_COMMAND_SCRIPT]

    if [ -x $DOMINO_CUSTOM_COMMAND_SCRIPT ]; then
      # execute custom command
      DebugText "-- before executing custom command"
      $DOMINO_CUSTOM_COMMAND_SCRIPT "$PARAM2" "$PARAM3" "$PARAM4" "$PARAM5" "$PARAM6"
      DebugText "-- after executing custom command"

    else
      DebugText "Invalid PARAM1:" [$PARAM1]
      echo 
      echo "Invalid command:" [$PARAM1]
      usage 
      exit 1
    fi
    echo 
    exit 0
    ;;

esac

rc_exit
